ios的自动转屏

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)x

在6以后,取代它的是

- (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations


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

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


应用的界面是


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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.autorotation = YES;
}

- (BOOL)shouldAutorotate
{
    return self.autorotation;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


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



然后开始实现按钮的方法

- (IBAction)changeFlag:(id)sender {
    if (_autorotation) {
        self.autorotation = NO;
        self.textLabel.text = @"Autorotation: No";
    } else {
        self.autorotation = YES;
        self.textLabel.text = @"Autorotation: Yes";
    }
}


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


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

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



原文地址:https://www.cnblogs.com/jiangu66/p/3172188.html