iOS控制屏幕隨意旋轉(zhuǎn)和狀態(tài)欄的控制

1、屏幕旋轉(zhuǎn)

使用有三個(gè)前提條件
1、根控制是UIViewController
2、根控制是UINavigationController
3、根控制是UITabBarController
注意::不同的根控制器情況下,使用方式不同。

你還需要了解的
// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
請(qǐng)注意, UIInterfaceOrientationLandscapeLeft 等于 UIDeviceOrientationLandscapeRight (反之亦然)

// This is because rotating the device to the left requires rotating the content to the right.
這是因?yàn)閷⒃O(shè)備向左旋轉(zhuǎn)需要將內(nèi)容向右旋轉(zhuǎn)
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

/* This exception is raised if supportedInterfaceOrientations returns 0, or if preferredInterfaceOrientationForPresentation
   returns an orientation that is not supported.
如果 supportedInterfaceOrientations 返回 0, 則引發(fā)此異常。如果preferredInterfaceOrientationForPresentation返回不支持的方向
*/
UIKIT_EXTERN NSExceptionName const UIApplicationInvalidInterfaceOrientationException NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

1、根控制是UIViewController

::直接在控制器中重寫下面的方法,設(shè)置是否支持自動(dòng)旋轉(zhuǎn),以及支持的方向即可

/** 是否允許自動(dòng)旋轉(zhuǎn) */
-(BOOL)shouldAutorotate
{
    return NO;
}
/** 支持的方向 */
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    return UIInterfaceOrientationMaskPortrait;
}

2、根控制是UINavigationController

::在根控制器下面寫

-(BOOL)shouldAutorotate
{
    if ([[self.viewControllers lastObject] isKindOfClass:["指定哪個(gè)控制器" class]]) {
        return YES;
    }
    return NO;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    if ([[self.viewControllers lastObject]isKindOfClass:["指定哪個(gè)控制器" class]]) {
                return UIInterfaceOrientationMaskLandscapeRight;
            }
    return UIInterfaceOrientationMaskPortrait;
}

3、根控制是UITabBarController

::在根控制器下面寫

1、在此使用通知,需要先定義一個(gè)全局的BOOL
@interface CustomTabBarController : UITabBarController
{
    BOOL _shouldAutorotate;
}
@end
2、在注冊(cè)通知
@implementation CustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(autorotateInterface:) name:@"InterfaceOrientationNotificationKey" object:nil];
}
- (void)autorotateInterface:(NSNotification *)notifition
{
    _shouldAutorotate = [notifition.object boolValue];
}
3、重寫方法
/**
 *
 *  @return 是否支持旋轉(zhuǎn)
 */
-(BOOL)shouldAutorotate
{
    return _shouldAutorotate;
}
/**
 *  適配旋轉(zhuǎn)的類型
 */
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    if (_shouldAutorotate) {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}
@end
4、需要在哪個(gè)頁面進(jìn)行旋轉(zhuǎn),則在那個(gè)頁面的需要發(fā)出通知
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UserDefaultNotificationCenter postNotificationName:@"InterfaceOrientationNotificationKey" object:@"1"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [UserDefaultNotificationCenter postNotificationName:@"InterfaceOrientationNotificationKey" object:@"0"];
}

注意::不同的根控制不能混用,如果你的根控制器是TabbarVC,使用了navVC的控制方法會(huì)出現(xiàn)當(dāng)前頁面有效,返回后的頁面就不受控制的問題。

2、狀態(tài)欄控制

需要特別注意::

info.plist文件中,View controller-based status bar appearance項(xiàng)設(shè)為YES,則View controller對(duì)status bar的設(shè)置優(yōu)先級(jí)高于application的設(shè)置。
View controller-based status bar appearance項(xiàng)設(shè)為NO,則以application的設(shè)置為準(zhǔn),view controller的prefersStatusBarHidden方法無效,是根本不會(huì)被調(diào)用的。

使用場(chǎng)景為一個(gè)playerVC控制器,加載playerV的控制功能來使用,并且playerV的控制結(jié)果要提現(xiàn)在playerVC上面。

@interface PlayerViewController : BaseViewController
/** 是否隱藏狀態(tài)欄 */
- (void)whetherHidenStateBar:(BOOL)isHidenStateBar;
@end
@interface PlayerView : UIView
/**  */
@property (nonatomic, weak) PlayerViewController  *playerVC;
@end
// 在控制器中使用playerV的時(shí)候,傳遞playerVC給PlayerView
- (void)setupPlayerView
{
    self.playerView = [[PlayerView alloc] init];
    self.playerView.playerVC = self;
    [self.view addSubView:self.playerView];
}
// 需要在控制器中才能使用,控制狀態(tài)欄的隱藏和顯示
- (BOOL)prefersStatusBarHidden
{
    return self.isHidenStateBar;
}
- (void)whetherHidenStateBar:(BOOL)isHidenStateBar
{
    self.isHidenStateBar  = isHidenStateBar;
    // 主動(dòng)調(diào)用也有效果
    [self prefersStatusBarHidden];
    // 橫屏的時(shí)候不寫這個(gè)會(huì)有點(diǎn)問題,測(cè)試的時(shí)候你也會(huì)發(fā)現(xiàn)
    [UIApplication sharedApplication].statusBarHidden = isHidenStateBar;
}
// 在playerView中需要使用到的地方調(diào)用
[self.playerVC whetherHidenStateBar:YES];或者[self.playerVC whetherHidenStateBar:NO];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容