欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS masonry的使用方法

 更新時間:2021年09月06日 10:34:19   作者:軒墨️  
這篇文章主要介紹了iOS masonry的基本使用方法的相關(guān)資料,文章還介紹了CocoaPods的安裝過程,需要的朋友可以參考下面文字內(nèi)容

iOS masonry的基本使用

前言:

在寫OC的UI時,當在不同的機型上運行時,如果只用frame則會導(dǎo)致視圖中的控件嚴重變形,這是因為不同機型的屏幕大小不一樣,所以這周學習了masonry,掌握了一些基本用法。在使用第三方庫Masonry之前,需要先安裝CocoaPods。

一、CocoaPods的安裝

安裝教程

安裝好后,創(chuàng)建一個工程“test2”,創(chuàng)建結(jié)束后在終端輸入以下代碼:

cd /Users/haoqianbiao/Desktop/test2  //文件的路徑

然后在終端輸入:

touch PodFile

之后我們的文件里就多了一個Podfile的文件

然后在該文件里輸入:

platform :ios, '7.0'
target 'test2' do
pod 'Masonry'
end
//target后面的單引號里是你工程的名字

最后一步是在終端讀取PodFile找到相關(guān)類庫下載并自動集成到項目中,同時生成新的*.xcworkspace文件:
之后就直接打開xcworkspace文件進行編程就可以了。

二、Masonry的基本使用

1、三個約束和基礎(chǔ)API

/添加新約束

- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

//更新約束,會覆蓋之前的約束

- (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

//完全移除舊約束,添加新約束(重置)

- (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

equalTo()       參數(shù)是對象類型,一般是視圖對象或者mas_width這樣的坐標系對象
mas_equalTo()   和上面功能相同,參數(shù)可以傳遞基礎(chǔ)數(shù)據(jù)類型對象,可以理解為比上面的API更強大

width()         用來表示寬度,例如代表view的寬度
mas_width()     用來獲取寬度的值。和上面的區(qū)別在于,一個代表某個坐標系對象,一個用來獲取坐標系對象的值

示例:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel* label = [[UILabel alloc] init];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
                            make.top.equalTo(self.view).offset(100);
                             make.size.mas_equalTo(CGSizeMake(200, 50));
    }];
    label.backgroundColor = [UIColor blackColor];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(label);
        make.top.equalTo(label.mas_bottom).offset(100);
        make.size.mas_equalTo(CGSizeMake(200, 50));
    }];
    [button setBackgroundColor:[UIColor yellowColor]];
    [button setTitle:@"更新約束" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

-(void) press:(UIButton*) btn {
    [btn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
}

效果:

 

 到此這篇關(guān)于iOS masonry的使用方法的文章就介紹到這了,更多相關(guān)iOS masonry的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論