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

iOS 常用第三方庫之 Masonry:用代碼寫約束也可以很輕鬆

編輯:iOS 開發綜合

一、前言

關於蘋果的布局方式,一直是不少開發者糾結的點:到底該用代碼寫約束,還是拖 storyboard?個人早期開發很少寫代碼約束,實在太囉嗦,所以一律選擇 AutoLayout 加拖線設置。但走進團隊協作就會發現,很多公司需要動態修改約束,甚至用約束做動畫,這時候 storyboard 反而不夠靈活——多人協作時 storyboard 的衝突也更難處理。於是越來越多人轉向純代碼開發,Masonry 也就跟著流行起來。它封裝了 AutoLayout 的冗長 API,讓寫約束這件事變得清爽。

二、引入 Masonry

Masonry 是第三方類庫,從官方倉庫下載後解壓,把 Masonry 文件夾拖進工程即可(記得勾選 Copy items if needed)。如果項目裡還沒有使用 CocoaPods,手動集成是最快的路徑。

三、開始使用

使用之前,建議在 AppDelegate 的啟動方法裡自己加載根控制器,把默認的 Main.storyboard 從項目中移除(若運行報錯,順手改一下啟動配置即可):

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    UINavigationController *nav = [[UINavigationController alloc]
                                   initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    return YES;
}

這樣就完全由代碼掌控界面層級了。

四、對比:原生 NSLayoutConstraint

先看不用 Masonry 時,實現「在視圖中添加一個子視圖並留出邊距」需要寫多少代碼:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Basic View";

    UIView *view1 = [[UIView alloc] init];
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    view1.backgroundColor = [UIColor greenColor];
    UIView *superview = self.view;
    [superview addSubview:view1];

    UIEdgeInsets padding = UIEdgeInsetsMake(74, 10, 10, 10);

    [superview addConstraints:@[
        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeTop
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeTop
                                    multiplier:1.0
                                      constant:padding.top],

        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeLeft
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeLeft
                                    multiplier:1.0
                                      constant:padding.left],

        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeBottom
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                      constant:-padding.bottom],

        [NSLayoutConstraint constraintWithItem:view1
                                     attribute:NSLayoutAttributeRight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:superview
                                     attribute:NSLayoutAttributeRight
                                    multiplier:1.0
                                      constant:-padding.right]
    ]];
}

四條約束、幾十行代碼,關鍵字重複到讓人麻木,稍微改個邊距還得數清楚哪裡要加負號。這正是 Masonry 要解決的問題——用鏈式語法把同樣的意圖壓縮成幾行:

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview).with.offset(padding.top);
    make.left.equalTo(superview).with.offset(padding.left);
    make.bottom.equalTo(superview).with.offset(-padding.bottom);
    make.right.equalTo(superview).with.offset(-padding.right);
}];

可讀性和維護成本的差距一目了然。

五、常用 API 速查

// 相對於父視圖
make.edges.equalTo(superview).insets(UIEdgeInsetsMake(10, 10, 10, 10));

// 固定尺寸
make.size.mas_equalTo(CGSizeMake(100, 50));

// 基於兄弟視圖
make.top.equalTo(otherView.mas_bottom).with.offset(10);

// 基於父視圖中心
make.center.equalTo(superview);

// 更新與重建約束
[self.boxView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.offset(50);
}];
[self.boxView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
}];

其中 mas_makeConstraints 用於初次添加,mas_updateConstraints 用於只改常數(更新已有約束),mas_remakeConstraints 則會先移除全部約束再重建——三者用途不同,混用容易得到意料之外的布局結果。

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