iOS 如何支持横竖屏

  1. 在工程文件中配置支持横竖屏功(Device Orientation),可以设置设备iPhone或iPad

  2. 在AppDelegate.h中添加以下属性

@property (nonatomic, assign) UIInterfaceOrientationMask interfaceOrientation;

3.在AppDelegate.m中实现代理方法:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    return self.interfaceOrientation;
}

4.在相应viewcontroller中调用appdelegate中interfaceOrientation属性,建议在viewWillAppear中调用,且在自定义的基类viewcontroller中使用,使用子类去重写该方法:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AppDelegate appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.interfaceOrientation = UIInterfaceOrientationMaskPortrait;  //  横屏
}

5.如果需要在某个页面修改横竖屏属性,请重复第4步

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容