iOS 应用整体竖屏,单个页面横屏

category实现

image.png

反向思维, 设置成全部可以横竖屏, category实现所有都不可以旋转横屏, 在单独需要横屏的controller用category实现可以横屏

有tabController需要实现一下代码
-(BOOL)shouldAutorotate
{
    return YES;
}


-(NSUInteger)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}


-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
有navController实现
-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}


-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
在你的baseController实现
- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
在需要横屏的controller实现
// 是否允许转屏
- (BOOL)shouldAutorotate
{
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    SEL selector = NSSelectorFromString(@"webView");
    UIWebView* webview= ((UIWebView *(*)(id, SEL))objc_msgSend)
    (self, selector);
    webview.backgroundColor = K_WHITE_COLOR;
    self.view.backgroundColor = K_WHITE_COLOR;
    webview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
//{
//    return (UIInterfaceOrientationLandscapeLeft == toInterfaceOrientation);
//}


- (BOOL)prefersStatusBarHidden {
    //    [super prefersStatusBarHidden];
    return NO;
}

/*
- (void)changeRotate:(NSNotification*)noti {
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
        || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
        //竖屏
        [self.navigationController setNavigationBarHidden: NO];
    } else {
        //横屏
        
        if (isIphoneX) {
            [self.navigationController setNavigationBarHidden: YES animated:YES];
        }else{
            [self.navigationController setNavigationBarHidden: NO];
        }
    }
}
*/

注意事项

- (BOOL)prefersStatusBarHidden {
    //    [super prefersStatusBarHidden];
    return NO;
}
  • 实现横屏后会出现隐藏导航栏的情况, 实现上面的方法, 在info.plist添加View controller-based status bar appearance NO
webview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  • 如果是present出来的话, 竖屏进入,竖屏出, 要不程序崩溃可以强制旋转成进来的方向
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector  = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
  • 适配横屏
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。