1.某个页面横屏显示
在appdelegate的代理方法supportedInterfaceOrientationsForWindow中添加相关代码:
//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
需要旋转的controller中添加如下:
@implementation HorizontalViewController
- (void)viewDidLoad {
[super viewDidLoad];
[selfcreadUI];
AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
appDelegate.allowRotation=1;
}
//点击返回按钮退出页面时恢复竖屏方式
- (void)back:(UIButton*)sender
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
appDelegate.allowRotation=0;
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
点击按钮,以模态视图的方式弹出横屏页面:
- (IBAction)signatureImageDidSelect:(id)sender {
WriteViewController *writeVc =[[WriteViewController alloc] init];
[self presentViewController:writeVc animated:YES completion:nil];
}
2.横屏下弹框闪退问题处理
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES'
防止横屏情况下发生的闪退情况,在需要横屏的controller中添加如下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
return NO;
}