您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

layoutSubviews 詳解:子視圖佈局與橫豎屏適配

編輯:iOS 開發綜合

概念

UIView 裡有個方法叫 layoutSubviews:

- (void)layoutSubviews;   // override point. called by layoutIfNeeded automatically.
// As of iOS 6.0, when constraints-based layout is used the base implementation
// applies the constraints-based layout, otherwise it does nothing.

它的職責是「重新佈局 subviews」。當你想更新子視圖的位置或大小時,重寫這個方法就是標準做法。系統預設實作什麼都不做,所以要用就必須自己覆寫,並記得呼叫 super。

實戰:橫豎屏不同佈局

iPad 上「好的應用」通常會為橫屏與豎屏設計不同的版面。下面兩張截圖來自同一個頁面的兩個方向——內容相同,佈局不同。

實作關鍵就在 layoutSubviews:當 view 設定為自動適配螢幕後,使用者旋轉裝置時系統會呼叫它,我們只要在裡面判斷方向、重設各子視圖的 frame 即可。

先做一个最精簡的原型:左邊一張圖,右邊一個「圖 + 文字」的子視圖。

結構上,我在主視圖放了一個 ContentView,再在 ContentView 裡放一個 ArticleView。右側的文字與綠色方塊是由 ArticleView 封裝的。兩個 view 的 xib 都開著:

在 ContentView 中判斷方向

-(void)layoutSubviews {
    [super layoutSubviews];
    UIDeviceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIDeviceOrientationPortrait ||
        interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        // 豎屏
        [self setVerticalFrame];
    } else if (interfaceOrientation == UIDeviceOrientationLandscapeLeft ||
               interfaceOrientation == UIDeviceOrientationLandscapeRight) {
        // 橫屏
        [self setHorizontalFrame];
    }
}

-(void)setVerticalFrame {
    NSLog(@"豎屏");
    [titleLable setFrame:CGRectMake(283, 0, 239, 83)];
    [leftView  setFrame:CGRectMake(38, 102, 384, 272)];
    [rightView setFrame:CGRectMake(450, 102, 282, 198)];
}

-(void)setHorizontalFrame {
    NSLog(@"橫屏");
    [titleLable setFrame:CGRectMake(183, 0, 239, 83)];
    [leftView  setFrame:CGRectMake(168, 122, 384, 272)];
    [rightView setFrame:CGRectMake(650, 122, 282, 198)];
}

在 ContentView 中裝入 ArticleView

因為用的是 xib,初始化入口是 initWithCoder::

-(id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        NSArray *arrayContentView = [[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil];
        rightView = [arrayContentView objectAtIndex:0];
        [self addSubview:rightView];
    }
    return self;
}

在 ArticleView 中同樣處理方向

-(void)layoutSubviews {
    [super layoutSubviews];
    UIDeviceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

    CGRect rect = self.frame;
    rect.size.width  = 282;
    rect.size.height = 198;
    [self setFrame:rect];

    if (interfaceOrientation == UIDeviceOrientationPortrait ||
        interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        [self setVerticalFrame];
    } else if (interfaceOrientation == UIDeviceOrientationLandscapeLeft ||
               interfaceOrientation == UIDeviceOrientationLandscapeRight) {
        [self setHorizontalFrame];
    }
}

-(void)setVerticalFrame {
    NSLog(@"豎屏");
    [contentView setFrame:CGRectMake(12, 6, 250, 125)];
    [textLable   setFrame:CGRectMake(50, 139, 182, 39)];
}

-(void)setHorizontalFrame {
    NSLog(@"橫屏");
    [contentView setFrame:CGRectMake(12, 6, 106, 158)];
    [textLable   setFrame:CGRectMake(135, 11, 147, 39)];
}

layoutSubviews 什麼時候會被呼叫

蘋果文件明確表示不該直接呼叫它來重新佈局,那它究竟何時觸發?整理如下:

· 手動呼叫(不建議,但確實會觸發)。

· 呼叫 setNeedsLayout 後於下一個週期,或 layoutIfNeeded 立即觸發。

· 呼叫 addSubview: 時。

· view 的 frame 改變時。

· 滑動 UIScrollView 時。

· 旋轉螢幕時,會觸發父 UIView 的 layoutSubviews。

· 改變某個 UIView 大小時,也會觸發其父檢視的 layoutSubviews。

要特別注意一個細節:當 view 的 frame 值為 0 時,addSubview 不會觸發 layoutSubviews。

最後談談這個範例本身的取捨。用 statusBarOrientation 判斷方向、再硬寫兩組座標,是最直覺但也是最脆弱的做法——一旦加入更多尺寸或 Localization,座標表會迅速膨脹。現行專案更合適的選擇是 Auto Layout + Size Class,或至少把方向判斷抽成一個獨立的 Category,讓 layoutSubviews 保持單純。理解 layoutSubviews 的觸發時機與 super 呼叫責任,才是這個方法真正的價值所在。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved