1.mas_equalTo和equalTo
默認情況下,mas_equalTo有自動包裝功能,比如自動將20包裝為@20
equalTo沒有自動包裝功能
如果添加了下面的宏,那么mas_equalTo和equalTo就沒有區別
#define MAS_SHORTHAND_GLOBALS
// 注意:這個宏一定要添加到#import "Masonry.h"前面
// 藍色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 尺寸限制:100x100
// 位置:粘著父控件右下角,間距是20
// 這個方法只會添加新的約束
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 寬度約束
make.width.equalTo(@100);
// 高度約束
make.height.equalTo(@100);
// 右邊
make.right.equalTo(self.view.mas_right).offset(-20);
// 頂部
make.top.equalTo(self.view.mas_top).offset(20);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 寬度約束
make.width.mas_equalTo(100);
// 高度約束
make.height.mas_equalTo(100);
// 右邊
make.right.equalTo(self.view).offset(-20);
// 頂部
make.top.equalTo(self.view).offset(20);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 寬度高度約束
make.width.height.mas_equalTo(100);
// 右邊
make.right.equalTo(self.view).offset(-20);
// 頂部
make.top.equalTo(self.view).offset(20);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 寬度高度約束
// make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 100)]);
// make.size.mas_equalTo(CGSizeMake(100, 100));
make.size.mas_equalTo(100);
// 右邊
make.right.equalTo(self.view).offset(-20);
// 頂部
make.top.equalTo(self.view).offset(20);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// 寬度高度約束
make.height.mas_equalTo(self.view).multipliedBy(0.5).offset(-50);
// 右邊
make.right.mas_equalTo(self.view).offset(-20);
// make.right.offset(-20);
// 頂部
make.top.mas_equalTo(self.view).offset(20);
// make.top.offset(20);
}];
2.width和mas_width
默認情況下,width是make對象的一個屬性,用來添加寬度約束用的,表示對寬度進行約束
mas_width是一個屬性值,用來當做equalTo的參數,表示某個控件的寬度屬性
如果添加了下面的宏,mas_width也可以寫成width
#define MAS_SHORTHAND
mas_height、mas_centerX以此類推
// 藍色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 紅色控件
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 添加約束
CGFloat margin = 20;
CGFloat height = 50;
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.left).offset(margin);
make.right.equalTo(redView.left).offset(-margin);
make.bottom.equalTo(self.view.bottom).offset(-margin);
make.height.equalTo(height);
make.top.equalTo(redView.top);
make.bottom.equalTo(redView.bottom);
make.width.equalTo(redView.width);
}];
[redView makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view.right).offset(-margin);
}];
3.以下方法都僅僅是為了提高可讀性,可有可無
- (MASConstraint *)with {
return self;
}
- (MASConstraint *)and {
return self;
}
4.約束的類型:
- 尺寸:width\height\size
- 邊界:left\leading\right\trailing\top\bottom
- 中心點:center\centerX\centerY
- 邊界:edges
- (void)test4
{
// 藍色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 添加約束
[blueView makeConstraints:^(MASConstraintMaker *make) {
// make.width.equalTo(self.view.width).multipliedBy(0.5);
// make.height.equalTo(self.view.height).multipliedBy(0.5).offset(-100);
make.width.equalTo(100);
make.height.equalTo(100);
make.centerX.equalTo(self.view.centerX);
make.centerY.equalTo(self.view.centerY);
}];
}
- (void)test3
{
// 藍色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 距離父控件四周都是50間距
// [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(self.view.mas_left).offset(50);
// make.right.mas_equalTo(self.view.mas_right).offset(-50);
// make.top.mas_equalTo(self.view.mas_top).offset(50);
// make.bottom.mas_equalTo(self.view.mas_bottom).offset(-50);
// }];
// [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(self.view).offset(50);
// make.right.mas_equalTo(self.view).offset(-50);
// make.top.mas_equalTo(self.view).offset(50);
// make.bottom.mas_equalTo(self.view).offset(-50);
// }];
// [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.offset(50);
// make.right.offset(-50);
// make.top.offset(50);
// make.bottom.offset(-50);
// }];
// [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.top.offset(50);
// make.right.bottom.offset(-50);
// }];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
make.center.mas_equalTo(self.view).insets(UIEdgeInsetsZero);
}];
}
- (void)test2
{
// 藍色控件
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 居中(水平+垂直)
// 尺寸是父控件的一半
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(self.view).multipliedBy(0.5);
make.center.mas_equalTo(self.view);
// make.centerX.mas_equalTo(self.view);
// make.centerY.mas_equalTo(self.view);
}];
}
5.remakeConstraints和updateConstraints
// 這個方法會將以前的所有約束刪掉,添加新的約束
[blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
// 這個方法將會覆蓋以前的某些特定的約束
[blueView mas_updateConstraints:^(MASConstraintMaker *make) {
}];
6.Masonry自動計算cell行高
(1)導入UITableViewCell+HYBMasonryAutoCellHeight和UITableView+HYBCacheHeight.h這個分類
(2)設置關鍵依賴:
要想自動計算出 cell 的行高,我們還需要指定以哪個視圖作為 cell 的最后一個視圖,比如我們最后要添加一條線,我們可以以這條線作為 hyb_lastViewInCell ,如果這條線還需要距離底部一定距離,那么可以設置 hyb_bottomOffsetToCell 。
一般在cell 的configCellWithModel: indexPath:方法中設置這兩個屬性。
這兩個屬性在分類中,直接可以使用。
- (void)configCellWithModel:(InteractCommentModel *)infoModel indexPath:(NSIndexPath *)indexPath {
self.infoModel = infoModel;
self.indexPath = indexPath;
[_headImg sd_setImageWithURL:[NSURL URLWithString:infoModel.commUserHeadPic] placeholderImage:[UIImage imageNamed:@"SZ_wodetouxiang"]];
if (![infoModel.commUserNick isEqualToString:@""]) {
_name.text = infoModel.commUserNick;
}else{
_name.text = @"匿名用戶";
}
_comment.text = infoModel.commContent;
_time.text = infoModel.commTime;
if ([infoModel.commUserLevel isEqualToString:@"2"]) {
//_commentButton.hidden = YES;
_mark.hidden = NO;
}
[_line mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
[_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
[_moreBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
_tableView.hidden = YES;
_moreBtn.hidden = YES;
_line.hidden = YES;
self.hyb_lastViewInCell = self.comment;
self.hyb_bottomOffsetToCell = 8;
}
(3)要計算行高,只需要在 UITableView 的計算行高的代理方法中調用此API即可:
/**
* 通過此方法來計算行高,需要在config中調用配置數據的API
*
* @param indexPath 必傳,對應的indexPath
* @param confi 必須要實現,且需要調用配置數據的API
*
* @return 計算的行高
*/
+ (CGFloat)hyb_heightForIndexPath:(NSIndexPath *)indexPathconfig:(HYBCellBlock)config;
在調用時, config 傳回來了 cell 對象,需要在調用處調用方法來配置好數據,才能正確地計算出 cell 的行高。通常是這樣調用的:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
InteractCommentModel * infoModel = self.listArry[indexPath.row];
CGFloat h = [BaseInteractCommentCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
BaseInteractCommentCell *cell = (BaseInteractCommentCell *)sourceCell;
[cell configCellWithModel:infoModel indexPath:indexPath];
} cache:^NSDictionary *{
NSDictionary *cache = @{kHYBCacheUniqueKey : infoModel.commId,
kHYBCacheStateKey : @"",
kHYBRecalculateForStateKey : @(infoModel.ShouldUpdateCache)};
infoModel.ShouldUpdateCache = YES;
return cache;
}];
return h;
}