手动转屏

ios7,碰到个需要手动调整状态栏方向的问题,于是调用了下面这段代码。

 
  1. //设置状态栏 横屏  
  2. [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];  



问题来了,死活就是没有效果。
经过一番寻找,发现是
UIviewController方法- (BOOL)shouldAutorotate 返回值为YES的时候是不生效的。


发现原因了,马上解决,成功?
NO,你太天真了。

立马发现了下个问题,覆写方法- (BOOL)shouldAutorotate,仍然未生效。
WTF?
好吧,公布答案吧:由于UIViewController放置在Navigation中,而由于Navigation不人性化的设计,navigation的- (BOOL)shouldAutorotate是不会根据显示ViewController的- (BOOL)shouldAutorotate设置的值来改变的。


附上终极解决办法:将下面这段代码贴在AppDelegate.m的最后面,这个时候Navigation就会根据你显示的ViewController改变返回值了,然后你再去ViewController中覆写方法,返回NO,这时候,方法生效了!bingo!

 
  1. @implementation UINavigationController (Rotation)  
  2.   
  3.   
  4. - (BOOL)shouldAutorotate  
  5. {  
  6.     return [[self.viewControllers lastObject] shouldAutorotate];  
  7. }  
  8.   
  9.   
  10. - (NSUInteger)supportedInterfaceOrientations  
  11. {  
  12.     return [[self.viewControllers lastObject] supportedInterfaceOrientations];  
  13. }  
  14.   
  15.   
  16. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  17.     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];  
  18. }  
  19. @end  

补充下:

iOS7 如果在 info文件中,加上一列View controller-based status bar appearance

用下面的方法可以轻松控制

 
    1. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];  
原文地址:https://www.cnblogs.com/liyufeng2013/p/6100363.html