【轉(zhuǎn)載XMG】05-Autolayout幾種常用的方法

使用代碼實(shí)現(xiàn)Autolayout的方法1

  • 創(chuàng)建約束
+(id)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;
* view1 :要約束的控件
* attr1 :約束的類型(做怎樣的約束)
* relation :與參照控件之間的關(guān)系
* view2 :參照的控件
* attr2 :約束的類型(做怎樣的約束)
* multiplier :乘數(shù)
* c :常量
  • 添加約束
- (void)addConstraint:(NSLayoutConstraint *)constraint;
- (void)addConstraints:(NSArray *)constraints;
  • 注意
    • 一定要在擁有父控件之后再添加約束
    • 關(guān)閉Autoresizing功能
    view.translatesAutoresizingMaskIntoConstraints = NO;
    

使用代碼實(shí)現(xiàn)Autolayout的方法2 - VFL

  • 使用VFL創(chuàng)建約束數(shù)組
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary *)metrics
views:(NSDictionary *)views;
* format :VFL語(yǔ)句
* opts :約束類型
* metrics :VFL語(yǔ)句中用到的具體數(shù)值
* views :VFL語(yǔ)句中用到的控件
  • 使用下面的宏來(lái)自動(dòng)生成views和metrics參數(shù)
NSDictionaryOfVariableBindings(...)

使用代碼實(shí)現(xiàn)Autolayout的方法3 - Masonry

  • 使用步驟
    • 添加Masonry文件夾的所有源代碼到項(xiàng)目中
    • 添加2個(gè)宏、導(dǎo)入主頭文件
    // 只要添加了這個(gè)宏,就不用帶mas_前綴
    #define MAS_SHORTHAND
    

// 只要添加了這個(gè)宏,equalTo就等價(jià)于mas_equalTo

define MAS_SHORTHAND_GLOBALS

// 這個(gè)頭文件一定要放在上面兩個(gè)宏的后面

import "Masonry.h"

```
  • 添加約束的方法
// 這個(gè)方法只會(huì)添加新的約束
 [view makeConstraints:^(MASConstraintMaker *make) {

 }];

// 這個(gè)方法會(huì)將以前的所有約束刪掉,添加新的約束
 [view remakeConstraints:^(MASConstraintMaker *make) {

 }];

 // 這個(gè)方法將會(huì)覆蓋以前的某些特定的約束
 [view updateConstraints:^(MASConstraintMaker *make) {

 }];
  • 約束的類型
1.尺寸:width\height\size
2.邊界:left\leading\right\trailing\top\bottom
3.中心點(diǎn):center\centerX\centerY
4.邊界:edges
  • tableView如何顯示數(shù)據(jù)
    • 設(shè)置dataSource數(shù)據(jù)源
    • 數(shù)據(jù)源要遵守UITableViewDataSource協(xié)議
    • 數(shù)據(jù)源要實(shí)現(xiàn)協(xié)議中的某些方法
/**
 *  告訴tableView一共有多少組數(shù)據(jù)
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

/**
 *  告訴tableView第section組有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

/**
 *  告訴tableView第indexPath行顯示怎樣的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

/**
 *  告訴tableView第section組的頭部標(biāo)題
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

/**
 *  告訴tableView第section組的尾部標(biāo)題
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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