在AppDelegate內實現函數如下:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ( [[sharedNavigator topViewController] isKindOfClass:NSClassFromString(@"SLWebViewController")] )
return UIInterfaceOrientationMaskAllButUpsideDown;
return UIInterfaceOrientationMaskPortrait;
}
SLWebViewController就是你需要支持橫屏展示的ViewController。
在SLWebViewController中實現如下接口以支持除UpsideDown外的顯示方式:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
添加UIDeviceOrientationDidChangeNotification監聽,橫屏時隱藏狀態欄和導航欄,最大化顯示高度。
#pragma mark - Reg & Unreg ntf
- (void)regNtf
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)unregNtf
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
#pragma mark - Ntf handler
- (void)onDeviceOrientationDidChangeNotification:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ( orientation == UIDeviceOrientationPortraitUpsideDown ) return;
if ( orientation == UIDeviceOrientationPortrait )
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else if ( orientation == UIDeviceOrientationLandscapeLeft )
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}