shouldAutorotateToInterfaceOrientation

关于屏幕旋转的问题:

iOS6之后

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
       return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

这个方法废弃啦,取而代之的是这俩个组合:
- (BOOL)shouldAutorotate
{
   return YES; //ios6之前默认为NO,ios6之后默认为YES
}
 
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

当然,为了保持对旧版本系统行为的兼容性,不要删掉shouldAutorotateToInterfaceOrientation方法。另外还有一个preferred朝向也可以加上,这个大概说的是横屏的时候屏幕的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;

}

简单说明:

UIInterfaceOrientationMaskLandscape  支持左右横屏
UIInterfaceOrientationMaskAll  支持四个方向旋转
UIInterfaceOrientationMaskAllButUpsideDown 支持除了UpsideDown(home键在上)以外的旋转

另外:

1.info.plist文件里 Supported interface orientations里面的值表示支持的旋转方向,可以添加也可以删除,iphone默认为三个方向(除了home键在上),ipad默认为四个方向,

2.旋转后如需重新绘图,有以下两种方法:

(1)在该view的属性视图中设置mode为Redraw

(2)在view里实现以下代码:

- (void)setUp //随便其他的方法名称也可以

{

    self.contentMode = UIViewContentModeRedraw; //设置为需要重新绘图

}

 

- (void)awakeFromNib //这种情况是当前view被移除后再次唤醒的时候调用

{

    [self setUp];

}

 

- (id)initWithFrame:(CGRect)frame //这种情况是第一次使用当前view的时候调用,view被移除后再次使用不会调用此方法,而是调用awakeFromNib

{

    self = [super initWithFrame:frame];

    if (self) {

        [self setUp];

    }

    return self;

}

原文地址:https://www.cnblogs.com/shadowflyer/p/3488415.html