iOS 横竖屏切换及某个界面横屏

2017-02-05  本文已影响0人  hallfrita

进入正文前先认识几个概念

portrait  竖屏(Home键在下边)

upside down  竖屏(Home键在上边)

landscape  横屏   |landscape left  横屏Home键在左边

|landscape right 横屏Home键在右边

1、先让窗口支持横竖屏

两种方法可以修改窗口对横竖屏的支持

一种,代码控制

在appDelegate中重写方法,比如

- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {

if(self.isShouAutoRotate) {

returnUIInterfaceOrientationMaskAll;

}

returnUIInterfaceOrientationMaskPortrait;

}

另一种就是,在【General】-->【Device Orientation】中设置好支持的方向

如图

两种方法的利弊,读者自己推敲。

2、下面认识三个方法

//是否自动旋转

- (BOOL)shouldAutorotate {

returnYES;

}

//返回支持的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskLandscapeRight;

}

//切换横竖屏时,可以重写这个方法,来重新布局界面

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}

3、个别界面横屏

第一种处理方式:   1中对应的设置支持横屏完成后,在相应的控制器直接实现2中的方法二

第二种处理方式:   1中对应的设置支持横屏完成后,在相应控制器的viewDidLoad方法中强制设置设备朝向,代码如下:

NSNumber*value = [NSNumbernumberWithInt:4];

[[UIDevicecurrentDevice]setValue:valueforKey:@"orientation"];

上一篇下一篇

猜你喜欢

热点阅读