iOS 橫屏豎屏自適應(yīng)總結(jié)

了解更多,請關(guān)注我的微信公眾號:mellong

所有frame的高度和寬度應(yīng)該通過superview的bounds計算。
xib中的view無法設(shè)置auto mask的必須通過代碼設(shè),不設(shè)定的話有時可以自動適應(yīng),但是有時會出現(xiàn)有部分黑屏的情況。
兩邊都不設(shè)置mask則為居中顯示。

以下兩方法為rotate是自動調(diào)用,如果該viewController沒有navigationController時,以下兩方法可能不被調(diào)用,需要自己加入通知中心。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

調(diào)用此方法時superview.bounds已經(jīng)改變。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

調(diào)用此方法時superview.bounds未改變

獲取當(dāng)前屏幕方向

UIInterfaceOrientation currentOrient = [UIApplication  sharedApplication].statusBarOrientation;

判斷當(dāng)前設(shè)備是否為4寸屏

#define isIPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

3.5與4寸屏高度相差88.f,寬度一樣為320.f
自適應(yīng)橫屏一般修改automask的autowidth,導(dǎo)航欄和一般控件主要變化的是寬度,高度也變化的一般是可以tableView和scrollView等。

有時候橫屏沒有正確自適應(yīng)一般是superview.bounds未改變,設(shè)置subview frame的時機不對。

  • 如果想讓某一個ViewController固定某個方向不旋轉(zhuǎn),方法如下:
  1. 修改AppDelegate.m,加入下列代碼,其中_enablePortrait為新增的變量,用于判斷是否要進行旋轉(zhuǎn)。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(_enablePortrait)
    {
        return UIInterfaceOrientationMaskPortrait;
    }
        
    return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;

}
  1. 在不需要旋轉(zhuǎn)的viewController中的下列方法中加入以下代碼即可。
-(void)viewWillAppear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;
    delegate.enablePortrait = YES;

}



- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;
    delegate.enablePortrait = NO;
}

由于使用pushViewController會導(dǎo)致所進入的視圖會根據(jù)前一視圖的方向顯示,所以需要用以下方法hack一下,才能使其自動根據(jù)設(shè)定的方向旋轉(zhuǎn)。

- (void)updateOrientation
{
    [[UIApplicationsharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitanimated:NO];
    UIViewController *viewController = [[UIViewControlleralloc] init];
    [self presentModalViewController:viewController animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    [viewController release];
}
  • iOS6旋轉(zhuǎn)發(fā)生當(dāng)時屏幕不旋轉(zhuǎn)的原因可能是:
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
            self.window.rootViewController = gameNavController;
        }else {
            [self.window addSubview:gameNavController.view];

        }

在應(yīng)用中有時需要制定某些頁面是Portrait或者landscape,這時需要在info.plist文件加入對這些方向的支持。
如果window的rootViewController是NavigationController則需繼承該類寫入:

//iOS6以下版本
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
//iOS6及以上版本
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;

}

由此則全局默認情況下只支持landscape。
注意:navigationController在其子類中指定,在push進去的viewController指定則是無效。

有效的情況為使用presentModalViewController或者其他形式的present,在present的viewController中重寫這三個方法,可以限制其當(dāng)前的方向只為portrait.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationPortrait == toInterfaceOrientation;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

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

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

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