iOS

iOS9之后强制横屏

2016-10-06  本文已影响160人  浮浅丶Superficial
1、IOS8之后有的方法写到类里强制横屏之后已经没有用了
- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
2、IOS8之后该怎么实现强制横屏

首先在代理类实现该方法:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.isForceLandscape) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else if (self.isForcePortrait){
        return UIInterfaceOrientationMaskPortrait;
    }
    //return UIInterfaceOrientationMaskAll;
    return UIInterfaceOrientationMaskPortrait;
}

然后在代理类头文件里定义2个全局变量

/** 横屏 */
@property (nonatomic, assign, getter=isForceLandscape) BOOL forceLandscape;
/** 竖屏 */
@property (nonatomic, assign, getter=isForcePortrait) BOOL forcePortrait;

最后一步,在你所需要实现强制横屏的ViewController里添加如下方法

/**
 *  强制横屏
 */
-(void)forceOrientationLandscape
{
    //这种方法,只能旋转屏幕不能达到强制横屏的效果
    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 = UIInterfaceOrientationMaskAllButUpsideDown;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    //加上代理类里的方法,旋转屏幕可以达到强制横屏的效果
    lvAppDelegate *appdelegate=(lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forceLandscape = YES;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    
}
/**
 *  强制竖屏
 */
-(void)forceOrientationPortrait
{
    //加上代理类里的方法,旋转屏幕可以达到强制竖屏的效果
    lvAppDelegate *appdelegate=(lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forcePortrait = YES;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self forceOrientationLandscape];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //释放约束
    lvAppDelegate *appdelegate = (lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forcePortrait = NO;
    appdelegate.forceLandscape = NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    //退出界面前恢复竖屏
    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 = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

/**
 *  屏幕旋转时从新布局子控件
 */
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.view layoutSubviews];
}
上一篇下一篇

猜你喜欢

热点阅读