最近在做一個(gè)只支持豎屏的項(xiàng)目剥纷,突然遇到了一個(gè)需要橫屏展示的頁(yè)面痹籍,以下是我查閱資料后實(shí)現(xiàn)的方法,這里做個(gè)記錄方便以后自己和大家使用筷畦。
只需要設(shè)置三個(gè)地方词裤,一個(gè)是AppDelegate文件,一個(gè)是需要橫屏的控制器文件鳖宾,這里假設(shè)為RotationVC吼砂,最后是RotationVC的上級(jí)頁(yè)面假設(shè)為PreviousVC。
步驟:
1.
1.1在AppDelegate.h文件添加屬性@property(assign,nonatomic)BOOL restrictRotation; ??
1.2在AppDelegate.m文件添加以下方法
#pragma mark --允許轉(zhuǎn)向
-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
if(self.restrictRotation==YES)
return UIInterfaceOrientationMaskLandscapeRight;
else
return UIInterfaceOrientationMaskPortrait;
}
2.
2.1 在RotaionVC.m中添加如下方法
/**
*設(shè)置屏幕旋轉(zhuǎn)
*
*@param restriction yes or no
*/
- (void)restrictRotation:(BOOL) restriction {
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation= restriction;
}
2.2
在viewDidLoad方法中添加如下代碼
[self restrictRotation:YES];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice]setValue:value forKey:@"orientation"];
*3.
最后是PreviousVC鼎文。添加如下代碼
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//豎屏
NSNumber*value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice]setValue:value forKey:@"orientation"];
}