1.某個(gè)頁(yè)面橫屏顯示
在appdelegate的代理方法supportedInterfaceOrientationsForWindow中添加相關(guān)代碼:
//AppDelegate.h中
@interfaceAppDelegate :UIResponder
@property (nonatomic,assign)NSInteger allowRotation;//是否允許橫屏
@end
//AppDelegate.m中
@implementation AppDelegate
- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
? ? if (_allowRotation == 1)? {
? ? ? ? return UIInterfaceOrientationMaskAll;
? ? }
? ? else{
? ? ? ? return (UIInterfaceOrientationMaskPortrait);
? ? }
}
@end
需要旋轉(zhuǎn)的controller中添加如下:
@implementation ?HorizontalViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? [selfcreadUI];
? ? AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
? ? appDelegate.allowRotation=1;
}
//點(diǎn)擊返回按鈕退出頁(yè)面時(shí)恢復(fù)豎屏方式
- (void)back:(UIButton*)sender
{
? ? AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
? ? appDelegate.allowRotation=0;
? ? [self dismissViewControllerAnimated:YES completion:nil];
}
@end
點(diǎn)擊按鈕,以模態(tài)視圖的方式彈出橫屏頁(yè)面:
- (IBAction)signatureImageDidSelect:(id)sender {
? ? WriteViewController *writeVc =[[WriteViewController alloc] init];
? ? [self presentViewController:writeVc animated:YES completion:nil];
}
2.橫屏下彈框閃退問(wèn)題處理
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES'
防止橫屏情況下發(fā)生的閃退情況,在需要橫屏的controller中添加如下代碼:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
? ? return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
-(NSUInteger)supportedInterfaceOrientations{
? ? return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
? ? return NO;
}