美文网首页知识点
以最少的代码为自己的app添加横屏界面

以最少的代码为自己的app添加横屏界面

作者: 奴良 | 来源:发表于2016-06-16 09:10 被阅读171次

内容如题。
一般手机应用中很少用到横屏,一般都是视频播放类,或者应用中查看时间轴之类的界面。那么如何在不动原来代码的基础上添加这些横屏界面的操作呢?

1.勾选设备方向

Orientation.png

2.为UINavigationController添加一个category

@interface UINavigationController (Autorotate)
@end

然后在.m文件中添加如下代码

- (BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

3.在需要横屏的界面添加如下代码

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

注意点

在显示横屏界面时候请一定要用present方法,至于为什么不用push,你可以试一下就知道了。

效果如图

效果.gif

相关文章

网友评论

    本文标题:以最少的代码为自己的app添加横屏界面

    本文链接:https://www.haomeiwen.com/subject/qugelttx.html