美文网首页
iOS竖屏切到横屏

iOS竖屏切到横屏

作者: manmangood | 来源:发表于2020-02-25 18:09 被阅读0次

1.AppDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,assign) BOOL shouldRote;

@end

-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    UIDevice *device = [UIDevice currentDevice];
    if(!_shouldRote|| device.orientation==UIDeviceOrientationPortraitUpsideDown)
        return UIInterfaceOrientationMaskPortrait;
    //页面只横屏
    return UIInterfaceOrientationMaskLandscapeRight;
    //页面支持横竖旋转
    //return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait;
}

2、竖屏推到横屏(只横屏)

AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    delegate.shouldRote=YES;
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
    Controller *controller = [[Controller alloc]init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
- (void)back
{
    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    delegate.shouldRote=NO;
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationPortrait] forKey:@"orientation"];
    [self.navigationController popViewControllerAnimated:YES];
}

3、一个页面可横竖屏旋转

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    delegate.shouldRote=YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    delegate.shouldRote=NO;
}

根据横竖屏更新UI

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    if (size.width > size.height) {
        NSLog(@"横屏");
    }
    else{
        NSLog(@"竖屏"); 
    }
}
-(void) viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ) {
    }else{
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ) {
    }else{
    }
}

相关文章

网友评论

      本文标题:iOS竖屏切到横屏

      本文链接:https://www.haomeiwen.com/subject/ngmwpctx.html