天天看點

iOS強制橫屏

在做視訊播放時需要視訊播放頁面強制橫屏,其他頁面依然隻支援豎屏,下面是使用過的兩種方式。

iOS強制橫屏的兩種方式:

第1種:設定狀态欄方向,然後vc.view設定transform旋轉。注意:VC需要設定為隻支援豎屏。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
    [UIView animateWithDuration:
                     animations:^{
                         self.view.transform = CGAffineTransformMakeRotation(M_PI/);
                         self.view.bounds = CGRectMake(, , [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);

                     }
                     completion:^(BOOL finished) {

                     }];
           

然後重寫VC螢幕旋轉方法為隻支援豎向:

#pragma mark //螢幕旋轉
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;//隻支援這一個方向(正常的方向)
}
           

第2種:強制橫屏的VC需支援螢幕旋轉,但隻支援橫向,其他vc都需要設定自己對螢幕方向的支援。

強制橫屏VC最好使用自動布局,或者重寫viewWillLayoutSubviews和viewDidLayoutSubviews,實作進行頁面布局。

需要強制橫屏的VC的螢幕方向支援代碼:

#pragma mark  螢幕旋轉
//iOS5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//iOS6+
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}
           

其他不需要支援橫屏的VC的代碼(建議放在BaseVC):

#pragma mark //螢幕旋轉
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;//隻支援這一個方向(正常的方向)
}
           

然後也是最關鍵的地方如果使用navgationController請使用其子類,并在子類種重寫螢幕旋轉方法:

#pragma mark //螢幕旋轉
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
        return  [self.topViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return  [self.topViewController shouldAutorotate];
    }
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return  [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;//隻支援這一個方向(正常的方向)
}
           

================================

做視訊播放時需強制橫屏,開始使用第一種方式實作,後因AirPlay選擇框顯示依然是豎屏方向,改用了第二種方式,如果可以最好使用第二種方式。