前言
最近在項(xiàng)目中有一個(gè)需求,就是進(jìn)入某一個(gè)頁(yè)面時(shí)要強(qiáng)制橫屏,其他界面不做限制,查了資料發(fā)現(xiàn)有些方法對(duì)我的需求只有一部分是可行的邓线,所以在此做一下記錄淌友。
針對(duì)present
首先要在需要橫屏的controller中實(shí)現(xiàn)系統(tǒng)的三個(gè)方法
#pragma mark 強(qiáng)制橫屏(針對(duì)present方式)
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}
其實(shí)在需要橫屏的controller中實(shí)現(xiàn)以上三個(gè)方法就能滿足橫屏了,但是如果不能滿足的話就需要實(shí)現(xiàn)下面的方法了骇陈。
在剛進(jìn)入頁(yè)面的時(shí)候?qū)ζ聊环较蜃鱿绿幚?/p>
只需要在viewwillappear中實(shí)現(xiàn)以下方法即可
- (void)orientationToPortrait:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];//前兩個(gè)參數(shù)已被target和selector占用
[invocation invoke];
}
針對(duì)push
在appdelegate中聲明一個(gè)成員變量震庭,然后實(shí)現(xiàn)以下方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (_allowRotation == 1) {
return UIInterfaceOrientationMaskLandscapeLeft;
}else if (_allowRotation == 2){
return UIInterfaceOrientationMaskPortrait;
}else {
return UIInterfaceOrientationMaskAll;
}
}
然后再你需要旋轉(zhuǎn)的controller中獲取appdelegate并設(shè)置_allowRotation為相應(yīng)的值即可,需要注意的是當(dāng)你退出當(dāng)前controller后需要把_allowRotation設(shè)置為其他值你雌。不過(guò)這個(gè)方法貌似對(duì)present方式無(wú)效器联。此方法比較簡(jiǎn)單,對(duì)一些比較復(fù)雜的需求可能會(huì)無(wú)效匪蝙。
監(jiān)測(cè)屏幕旋轉(zhuǎn)
如果有些頁(yè)面需要根據(jù)屏幕旋轉(zhuǎn)重新布局可以監(jiān)測(cè)屏幕旋轉(zhuǎn)
注冊(cè)通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDeviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
監(jiān)測(cè)方法
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation
{
UIDevice *device = [UIDevice currentDevice] ;
switch (device.orientation) {
case UIDeviceOrientationFaceUp:
NSLog(@"屏幕朝上平躺");
break;
case UIDeviceOrientationFaceDown:
NSLog(@"屏幕朝下平躺");
break;
case UIDeviceOrientationUnknown:
NSLog(@"未知方向");
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"屏幕向左橫置");
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"屏幕向右橫置");
break;
case UIDeviceOrientationPortrait:
NSLog(@"屏幕直立");
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"屏幕直立主籍,上下顛倒");
break;
default:
NSLog(@"無(wú)法辨識(shí)");
break;
}
}
移除通知
退出頁(yè)面的時(shí)候移除通知
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];
方法肯定不止一種,目前只總結(jié)了對(duì)我有用的逛球,后期會(huì)補(bǔ)充千元。
如果文中有錯(cuò)誤,歡迎指正颤绕。