ios的自动转屏

来源:互联网 发布:数据挖掘聚类 编辑:程序博客网 时间:2024/05/20 08:43

在IOS6以前,设置转屏需要用到方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)x

在6以后,取代它的是

- (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations


在论坛上看到个问题,如何用按钮控制自动转屏

可以在相应的Controller中加入一个属性,一个BOOL型的变量autorotation


应用的界面是


然后再初始化的时候初始为YES,在自动转屏方法中return这个变量即可

[plain] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     self.autorotation = YES;  
  6. }  
  7.   
  8. - (BOOL)shouldAutorotate  
  9. {  
  10.     return self.autorotation;  
  11. }  
  12.   
  13. - (NSUInteger)supportedInterfaceOrientations  
  14. {  
  15.     return UIInterfaceOrientationMaskAll;  
  16. }  

这个项目允许的转屏模式主要由项目信息中的设置决定的



然后开始实现按钮的方法

[plain] view plaincopy
  1. - (IBAction)changeFlag:(id)sender {  
  2.     if (_autorotation) {  
  3.         self.autorotation = NO;  
  4.         self.textLabel.text = @"Autorotation: No";  
  5.     } else {  
  6.         self.autorotation = YES;  
  7.         self.textLabel.text = @"Autorotation: Yes";  
  8.     }  
  9. }  

这样就可以在点击按钮时更改是否允许转屏,以及Label中的text了。


另外,在IOS6以后自动缩放的方框默认不显示了,是因为加入了autolayout且默认为勾选状态的

取消勾选后Autosizing就显示出来了



原创粉丝点击