setStatusBarOrientation 未生效的解决办法

 

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

//设置状态栏 横屏
[[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!

 

 

 

 

 

 

 

 

@implementation UINavigationController (Rotation)


- (BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}


- (NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

 

补充下:

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

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

 

       [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值