iOS controller橫屏與豎屏的實(shí)現(xiàn)

關(guān)于橫豎屏一直是一個(gè)坑爹的東西.
但是蘋果還是不錯(cuò)的,好多東西都給了可視化的菜單,還是比較不錯(cuò)的!

橫豎屏.png

上圖是設(shè)置應(yīng)用都支持哪幾個(gè)方向的勾選菜單!

//  默認(rèn)情況,home鍵在下
UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
// home鍵在上
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
// home鍵在左
 UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
// home鍵在右
UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

1.強(qiáng)制橫屏(只在modal視圖的時(shí)候有效)

適用于一些強(qiáng)制橫屏的播放器,或者某些特殊的設(shè)計(jì)頁面

#pragma mark --
#pragma mark  強(qiáng)制橫屏代碼
- (BOOL)shouldAutorotate
{
    //是否支持轉(zhuǎn)屏
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //支持哪些轉(zhuǎn)屏方向
    return UIInterfaceOrientationMaskLandscape;
}

//進(jìn)入界面直接旋轉(zhuǎn)的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
// 是否隱藏狀態(tài)欄
- (BOOL)prefersStatusBarHidden
{
    return NO;
}

2.在代碼中,讓應(yīng)用中的部分界面自動(dòng)跟著手機(jī)旋轉(zhuǎn)旋轉(zhuǎn)!(push和modal都有效)

適用于很多情況!!!

2.1第一步勾選,只用支持的旋轉(zhuǎn)方向
旋轉(zhuǎn)方向.png

如果設(shè)計(jì)要求,值旋轉(zhuǎn)左右的某一個(gè)方向,請自行選擇!!!

2.2第二步,應(yīng)用中肯定會(huì)有navVC 或者 tabVC,設(shè)置代碼,因?yàn)閚avVC和tabVC的工作原理不一樣,所以代碼也不一樣

navVC中的代碼(nav是存在棧里的VC)

-(BOOL)shouldAutorotate
{  //允許旋轉(zhuǎn)
     return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{   //返回nav棧中的最后一個(gè)對象支持的旋轉(zhuǎn)方向
     return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{    // 返回nav棧中最后一個(gè)對象,堅(jiān)持旋轉(zhuǎn)的方向
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}

tabVC代碼(tab是選中的VC)

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

設(shè)置完子視圖控制器容器的旋轉(zhuǎn)狀態(tài)和方向之后,現(xiàn)在是允許所有的子控制器都可以進(jìn)行旋轉(zhuǎn)!

2.3然后我們需要設(shè)置不想進(jìn)行旋轉(zhuǎn)的控制器就OK了,想進(jìn)行旋轉(zhuǎn)的VC什么都不用設(shè)置!
- (BOOL)shouldAutorotate
{    // 不允許進(jìn)行旋轉(zhuǎn)
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{    // 返回默認(rèn)情況
    return UIInterfaceOrientationMaskPortrait ;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{    // 返回默認(rèn)情況
    return UIInterfaceOrientationPortrait;
}

3.使用單例去控制navVC或者tabVC是否支持旋轉(zhuǎn),來實(shí)現(xiàn)界面的旋轉(zhuǎn)

3.1勾選的地方全部勾掉
3.1.png
3.2創(chuàng)建單例,加一個(gè)屬性是否可以旋轉(zhuǎn)
@interface Single : NSObject

@property (nonatomic, assign) BOOL isRoat;

+ (Single *)single;

@end
3.3設(shè)置navVC和tabVC里面的代碼
- (BOOL)shouldAutorotate{
    return [Single single].isRoat;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 這里可以選擇任意的方向
    return UIInterfaceOrientationMaskAll;
}
3.4在你想要橫屏的界面代碼設(shè)置
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [Single single].isRoat = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [Single single].isRoat = NO;
}

方法二的原理就是,先設(shè)置應(yīng)用支持任意方向旋轉(zhuǎn),然后去設(shè)置子控制器是否可以旋轉(zhuǎn),因?yàn)槿绻鹡avVC或者tabVC不支持旋轉(zhuǎn),讓自控制器去實(shí)現(xiàn)旋轉(zhuǎn)是一件非誠復(fù)雜而且容易出bug的事情!
小建議:在寫項(xiàng)目的時(shí)候,為我們的不同旋轉(zhuǎn)方向的VC各創(chuàng)建一個(gè)基類VC,然后新創(chuàng)建的VC繼承于基類,這樣會(huì)少寫很多代碼!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容