iOS橫屏開發(fā)

一.指定某個(gè)頁面橫屏豎屏。

系統(tǒng)支持橫屏順序
默認(rèn)讀取plist里面設(shè)置的方向(優(yōu)先級(jí)最低)
application設(shè)置的級(jí)別次之
然后是UINavigationcontroller
級(jí)別最高的是UIViewController
例如:A界面跳轉(zhuǎn)到B界面,A界面是豎屏的,B界面進(jìn)入就要橫屏。

方案一:

1.首先設(shè)置項(xiàng)目 支持的屏幕方向
如果不選擇左或者右那么頁面想要左右橫屏都是不可能的,先全局設(shè)置橫屏能力。


1.png

2.在項(xiàng)目繼承了UINavigationController的子類CusNavigationController中重寫方法:shouldAutorotate和supportedInterfaceOrientation

//支持旋轉(zhuǎn)
-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

3.界面A中,重寫旋轉(zhuǎn)方法 和 支持的方向

-(BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

4.界面A跳轉(zhuǎn)界面B的方法

ViewController2 * vc=[[ViewController2 alloc]init];
[self presentViewController:vc animated:YES completion:nil];
//[self.navigationController pushViewController:vc animated:YES];

5.界面B重寫 旋轉(zhuǎn)方法 和 支持的方向

-(BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}
//一開始的方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return  UIInterfaceOrientationLandscapeLeft;
}

這個(gè)方式有個(gè)缺點(diǎn)就是跳轉(zhuǎn)方式只能是model生效,push不生效。

補(bǔ)充說明:測試發(fā)現(xiàn)如果打開鎖屏功能,之前不實(shí)現(xiàn)兩個(gè)繼承的shouldAutorotate和supportedInterfaceOrientations方法的頁面會(huì)出現(xiàn)不適配的橫屏問題。解決方法就是在CusNavigationController的shouldAutorotate方法中返回NO,保證所有ViewController都是不可旋轉(zhuǎn)的。A頁面可以不重寫這兩個(gè)方法,B頁面支持旋轉(zhuǎn)就行。

方案二:

利用KVC調(diào)用私有方法-橫屏

if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)])
    {
        SEL seletor=NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:seletor]];
        [invocation setSelector:seletor];
        [invocation setTarget:[UIDevice currentDevice]];
        int val=UIInterfaceOrientationLandscapeRight;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }

二.監(jiān)聽橫豎屏狀態(tài)并適配UI

使用通知監(jiān)聽

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:UIDeviceOrientationDidChangeNotification object:nil];

監(jiān)聽方法

-(void)change:(NSNotification*)notification
{
    CGFloat width=[UIScreen mainScreen].bounds.size.width;
    CGFloat height=[UIScreen mainScreen].bounds.size.height;
    if(width/height<1.0)
    {
        NSLog(@"豎屏");
        placeView.frame=CGRectMake(0, 0, 50, self.view.frame.size.height);
    }else
    {
        NSLog(@"橫屏");
        placeView.frame=CGRectMake(0, 0, 100, self.view.frame.size.height);
    }
}

三.強(qiáng)制橫屏其實(shí)就暴力的一句

          if(self.isScreenRight)
            {
                [[UIDevice currentDevice]setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
            }else
            {
                [[UIDevice currentDevice]setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
            }
最后編輯于
?著作權(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)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,201評(píng)論 3 119
  • 專業(yè)考題類型管理運(yùn)行工作負(fù)責(zé)人一般作業(yè)考題內(nèi)容選項(xiàng)A選項(xiàng)B選項(xiàng)C選項(xiàng)D選項(xiàng)E選項(xiàng)F正確答案 變電單選GYSZ本規(guī)程...
    小白兔去釣魚閱讀 10,513評(píng)論 0 13
  • 我曾經(jīng)也和你們一樣 急促地呼吸 揮汗如雨 我曾經(jīng)也和你們一樣 美麗的年紀(jì) 經(jīng)歷相同 我曾經(jīng)也和你們一樣 因忙碌而充...
    落日不晚閱讀 184評(píng)論 0 0
  • 距離6月30號(hào)已經(jīng)過去3天了,也就是說,閨女已經(jīng)開始了她最最喜歡的暑假3天了,本以為每天都會(huì)在一片混亂中度過,結(jié)果...
    神氣的莫小跳閱讀 417評(píng)論 1 0
  • 我們那會(huì)兒沒有學(xué)生用手機(jī),諾基亞還是手機(jī)中的戰(zhàn)斗機(jī),是大哥大,愛立信還沒和索尼合并,出了一個(gè)翻蓋型的就貴得嚇人...
    精進(jìn)的醫(yī)生閱讀 371評(píng)論 0 1

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