setStatusBarOrientation 未生效的解决办法

本文介绍了一个终极解决方案,通过在AppDelegate.m文件中添加特定代码段,使UINavigationController能够正确响应UIViewController的旋转设置。此方法允许开发者在UIViewController中覆盖旋转行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIviewController方法- (BOOL)shouldAutorotate 返回值为YES的时候是不生效的。


由于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  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值