最近在休整iOS7對於statusbar的兼容問題。
先來看看網上同胞們給出的答案:
1.總結的最全面的要數http://stackoverflow.com/questions/18294872/ios-7-status-bar-back-to-ios-6-style
這篇文章總結的固然很好,但終究是少了些例子。而且是英文的,相信不少和我一樣英文不好的,看得是一頭霧水。不過增加些概念方面得知識還是有必要的。
2. 自己看到的其他人的解決辦法以及自己嘗試的結果,大概是下面這樣子的,歡迎拍磚交流。
第一種:
首先,需要在Info.plist配置文件中,增加鍵:UIViewControllerBasedStatusBarAppearance,並設置爲YES;
然後,在UIViewController子類中實現以下兩個方法:
1 |
- (UIStatusBarStyle)preferredStatusBarStyle |
3 |
return UIStatusBarStyleLightContent; |
6 |
- ( BOOL )prefersStatusBarHidden |
最後,在需要刷新狀態欄樣式的時候,調用[self setNeedsStatusBarAppearanceUpdate]方法即可刷新
這種方法本人覺得不是很好(不知道是否方法部隊),能解決問題,但對於沒有基類的程序(架構不是太好的應用),未免很繁瑣,需要進行追個處理。
第二種:
首先,需要在Info.plist配置文件中,增加一下兩個鍵值對。
View controller-based status bar appearance > NO
Status bar style > UIStatusBarStyleLightContent (simple string)(這個我本人建議還是加在代碼中比較好,如果代碼中有相應的處理,其他有地方設置部隊,相應的controller將不受影響)
然後,在相關的controller中進行修改。
比如:navigationcontroller需要增加一個64pix的圖片,或者修改其高度
其他普通的controller,本人的處理辦法是加入代碼:
01 |
- ( void )viewDidAppear:( BOOL )animated |
03 |
[super viewDidAppear:NO]; |
04 |
CGRect frame = self.view.frame; |
05 |
if ([[UIDevice currentDevice] isOS7Version]) { |
07 |
frame.size.height = self.view.frame.size.height - 20; |
08 |
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; |
12 |
self.view.frame = frame; |
第三種:
直接設置window,因爲在ios7中statusbar是浮在上面的,那麼我們就將window向下移20pix。從理論上講,我很認同這種方法,但沒有嘗試成功。如有那位大神成功了嘗試,多謝能指教。
http://stackoverflow.com/questions/18886195/ios-7-status-bar-overlapping-ui
3. 在這裏鄙人還要補充的一點是UIImagePickerController對於statusbar的修改。這點曾糾結了我一個多小時,後來看到了有人如下解決:
01 |
- ( void )navigationController:(UINavigationController *)navigationController |
02 |
willShowViewController:(UIViewController *)viewController |
03 |
animated:( BOOL )animated { |
05 |
if ([navigationController isKindOfClass:[UIImagePickerController class ]] &&
|
06 |
((UIImagePickerController *)navigationController).sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { |
07 |
[[UIApplication sharedApplication] setStatusBarHidden:NO]; |
08 |
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO]; |
很好的解決了我的問題。參考鏈接如下:
http://stackoverflow.com/questions/2982095/is-there-a-way-to-prevent-uiimagepickercontroller-change-statusbar-style
還是先輩們總結的到位呀:又發現了一篇好文章http://www.vinqon.com/codeblog/?detail/11109