Iphone和iPad适配, 横竖屏

本文详细介绍了如何在iOS和iPad设备上实现应用的适配和自动旋转功能,包括设置Storyboard文件、使用UIViewController和UIWindow的适配方法、以及针对TabBarController和NavigationController根控制器的实现细节。

竖屏情况下: [UIScreen mainScreen].bounds.size.width = 320

[UIScreen mainScreen].bounds.size.width = 568

横屏情况下:

 [UIScreen mainScreen].bounds.size.width = 568

 [UIScreen mainScreen].bounds.size.height = 320

UIViewController 和 UIWindow同理

 

1. storyboard iphone和 iPad版

即为iphone和ipad分别创建xib/storyboard文件

Main_iPhone.storyboard

Main_iPad.storyboard

 

横竖屏

苹果官方文档:

When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns YES.

- (BOOL)shouldAutorotate

{   
  
return YES; } - (NSUInteger)supportedInterfaceOrientations {   return UIInterfaceOrientationMaskAll; }

注意要写在最顶层视图控制器, (self.window.rootViewController) , 否则不会进入以上方法

而且

supportedInterfaceOrientations的返回值必须是

UIInterfaceOrientationMaskLandscapeLeft 或者 UIInterfaceOrientationMaskLandscapeRight 或者...

总之必须加Mask

 通知是当前线程异步

 

tabbarcontroller 为 根控制器的情况

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

navigationcontroller为根控制器的情况:

- (BOOL)shouldAutorotate
{
    return [self.viewControllers.lastObject shouldAutorotate];
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}


- (NSUInteger)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}

 

转载于:https://www.cnblogs.com/apem/p/4427328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值