1. 程式人生 > >【iOS開發】---- iOS自動佈局(一)

【iOS開發】---- iOS自動佈局(一)

問題

你想將一個UI 元件放置到螢幕的中央。換句話說,你想你想將一個檢視放置到其父檢視的中央位置,使用限制條件。

方案

建立兩個限制條件:一個是將目標檢視的center.x 位置排列在其父檢視的center.x 位置,並且另外一個是將目標檢視的center.y 位置排列在其父檢視的center.y 位置

首先通過建立一個簡單的按鈕來開始,這個按鈕是將要放置在螢幕的中心的。所要做的就是確保按鈕的中央的x 與y 座標和按鈕將要放置的檢視的中央的x 和y 座標一致就行。所以在這裡我們將會建立兩個限制條件並將它們新增到這兩個按鈕的父檢視中。這裡有段示例程式碼:

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    /* 1) Create our button*/
	self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.button setTitle:@"button" forState:UIControlStateNormal];
    [self.button setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:self.button];
    
    UIView *superView = self.button.superview;
    
    /* 2) Create the constraint to put the button horizontally in the center */
    NSLayoutConstraint *centerXContraint = [NSLayoutConstraint constraintWithItem:self.button
                                                                        attribute:NSLayoutAttributeCenterX
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:superView
                                                                        attribute:NSLayoutAttributeCenterX
                                                                       multiplier:1.0
                                                                         constant:0];
    
    /* 3) Create the constraint to put the button vertically in the center */
    NSLayoutConstraint *centerYContraint = [NSLayoutConstraint constraintWithItem:self.button
                                                                        attribute:NSLayoutAttributeCenterY
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:superView
                                                                        attribute:NSLayoutAttributeCenterY
                                                                       multiplier:1.0
                                                                         constant:0];

    /* Add the constants to the superview of the button */
    [superView addConstraints:@[centerXContraint,centerYContraint]];
}
/* Suport rotation of device to all orientation */
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end

這樣在任意方向,你的檢視都會在螢幕的正中央了。

在button的父檢視新增屬性之前,呼叫了[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];這行程式碼。

下面是文件的解釋(重點在黑色文字):

setTranslatesAutoresizingMaskIntoConstraints:

Sets whether the view’s autoresizing mask should be translated into constraints for the constraint-based layout system.

設定view的自適應是否應該翻譯成 基於約束的佈局系統 的限制。

- (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag

Parameters

flag

YES if the view’s autoresizing mask should be translated into constraints for the constraint-based layout system, NO otherwise.

Discussion

Because the autoresizing mask naturally gives rise to constraints that fully specify a view’s position, any view that you wish to apply more flexible constraints to must be set to ignore its autoresizing mask using this method.
You should call this method yourself for programmatically created views. Views created using a tool that allows setting constraints should have this set already.

因為自適應自然產生了 完整的指定檢視位置的限制。任何你希望申請更多靈活約束的檢視必須用這個方法來設定忽略它的自適應。你需要在程式設計建立檢視時呼叫這個方法。被建立的檢視 使用允許設定約束的工具需要這個方法已經設定好。

好吧,其實主要就是說如果你使用了自動佈局,最好取消自適應

很多事情可以通過Auto Layout 來完成。但是,隨著你的深入研究,你就越發現自定佈局的設定意味著會產生更多的NSLayoutConstraint型別的限制條件。你會注意到你的程式碼越來越龐大,並變得越來越難以維護。因此,蘋果已經建立了Visual Format language(視覺化格式語言),通過它你可以使用簡單的ASCII 碼來表達你的限制條件。

下一篇將學習用Visual Format language來實現自動佈局。

         文中主要內容和程式碼均來自《iOS 6 Programming Cookbook》,感謝DevDiv熱心網友自發組織翻譯。