[iOS]Masonry简单约束
程序员文章站
2022-06-08 16:31:31
...
DEMO:https://download.csdn.net/download/u012881779/10565140
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface MasViewController : BaseViewController
@end
#import "MasViewController.h"
#import "Masonry.h"
#import "MasTableViewCell.h"
@interface MasViewController () <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataMArr;
@end
@implementation MasViewController
- (void)viewDidLoad {
[super viewDidLoad];
/* 头部 */
UIView *headerView = [UIView new];
headerView.backgroundColor = [UIColor redColor];
[self.view addSubview:headerView];
// 添加约束,在使用Masonry添加约束之前,需要在addSubview之后才能使用
[headerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.mas_left).with.offset(10);
make.right.mas_equalTo(-10);
make.top.mas_equalTo(self.navHigh+10);
make.height.mas_equalTo(84);
}];
/* 头部的背景 */
UIView *contentView = [UIView new];
contentView.backgroundColor = [UIColor greenColor];
[headerView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
// UIEdgeInsets insets = {top, left, bottom, right};
make.edges.mas_equalTo(headerView).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
/* 头部图片 */
UIImageView *photoIV = [UIImageView new];
[photoIV setBackgroundColor:[UIColor blueColor]];
[contentView addSubview:photoIV];
[photoIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(contentView);
make.left.mas_equalTo(10);
make.size.mas_equalTo(CGSizeMake(42, 42));
}];
/* 头部文字 */
UILabel *lab = [UILabel new];
lab.backgroundColor = [UIColor orangeColor];
lab.text = @" UILabel";
[contentView addSubview:lab];
// 移除约束,重新添加新的约束
[lab mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(photoIV.mas_right);
make.top.mas_equalTo(contentView).with.offset(10);
// 添加相对于父视图的约束 可以直接传递基础数据类型对象
make.right.mas_equalTo(-10);
make.bottom.mas_equalTo(-10);
}];
// 更新约束,其它约束保持不变
[lab mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(photoIV.mas_right).with.offset(10);
}];
/* 分隔线 */
UIView *lineView = [UIView new];
lineView.backgroundColor = [UIColor grayColor];
[self.view addSubview:lineView];
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(headerView.mas_bottom).offset(10);
make.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
make.height.mas_equalTo(0.5);
}];
/* 列表 */
if (!_tableView) {
_tableView = [UITableView new];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_tableView];
[_tableView setSeparatorStyle:NO];
_tableView.estimatedRowHeight = 100;
_tableView.rowHeight = UITableViewAutomaticDimension;
}
[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(lineView.mas_bottom);
make.right.mas_equalTo(0);
make.bottom.mas_equalTo(0);
}];
if (!_dataMArr) {
_dataMArr = [NSMutableArray new];
}
NSMutableDictionary *tempMDict = [NSMutableDictionary new];
[tempMDict setObject:@"标题" forKey:@"title"];
[tempMDict setObject:@"内容内容内容内容内容内容内容内容内容内容内容内容内容内容" forKey:@"content"];
[tempMDict setObject:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2234153389,1995395275&fm=27&gp=0.jpg" forKey:@"picture"];
[_dataMArr addObject:tempMDict];
[_tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataMArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MasTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"MasTableViewCell"];
if (cell == nil){
cell = [[MasTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MasTableViewCell"];
}
[cell reloadCellWithData:_dataMArr[indexPath.row]];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end
#import <UIKit/UIKit.h>
@interface MasTableViewCell : UITableViewCell
@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UIImageView *markIV;
@property (strong, nonatomic) UILabel *titleLab;
@property (strong, nonatomic) UILabel *contentLab;
@property (strong, nonatomic) UIView *lineView;
- (void)reloadCellWithData:(NSDictionary *)tempData;
@end
#import "MasTableViewCell.h"
#import "Masonry.h"
@implementation MasTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)reloadCellWithData:(NSDictionary *)tempData {
if (tempData) {
[self initViewAction];
self.titleLab.text = [tempData objectForKey:@"title"];
self.contentLab.text = [tempData objectForKey:@"content"];
NSString *picPath = [tempData objectForKey:@"picture"];
NSData *picData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picPath]];
[self.markIV setImage:[UIImage imageWithData:picData]];
}
}
- (void)initViewAction {
if (!self.bgView) {
self.bgView = [UIView new];
[self.contentView addSubview:self.bgView];
}
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
// UIEdgeInsets insets = {top, left, bottom, right};
make.edges.mas_equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
}];
if (!self.markIV) {
self.markIV = [UIImageView new];
[self.bgView addSubview:self.markIV];
self.markIV.backgroundColor = [UIColor blueColor];
}
[self.markIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.top.mas_equalTo(10);
make.width.mas_equalTo(64);
// UIImageView约束底边的话会根据图片自适应高度
// make.bottom.mas_equalTo(-10);
make.height.mas_equalTo(64);
}];
if (!self.titleLab) {
self.titleLab = [UILabel new];
self.titleLab.numberOfLines = 0;
[self.bgView addSubview:self.titleLab];
}
if (!self.contentLab) {
self.contentLab = [UILabel new];
self.contentLab.numberOfLines = 0;
[self.bgView addSubview:self.contentLab];
}
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.markIV.mas_right).offset(10);
make.top.mas_equalTo(10);
make.bottom.mas_equalTo(self.contentLab.mas_top).offset(-5);
make.right.mas_equalTo(-10);
}];
[self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.markIV.mas_right).offset(10);
make.top.mas_equalTo(self.titleLab.mas_bottom).offset(5);
make.bottom.mas_equalTo(-10);
make.right.mas_equalTo(-10);
}];
if (!self.lineView) {
self.lineView = [UIView new];
self.lineView.backgroundColor = [UIColor grayColor];
[self.bgView addSubview:self.lineView];
}
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
make.bottom.mas_equalTo(0);
make.height.mas_equalTo(0.5);
}];
}
@end
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
@property (assign, nonatomic) float navHigh;
@end
#import "BaseViewController.h"
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// (statusbar)
CGRect rectOfStatusbar = [[UIApplication sharedApplication] statusBarFrame];
//(navigationbar)
CGRect rectOfNavigationbar = self.navigationController.navigationBar.frame;
_navHigh = rectOfStatusbar.size.height + rectOfNavigationbar.size.height;
}
@end
上一篇: AutoLayout 安卓全新的适配方式
下一篇: 为UILabel创建链式调用写法