使用 Masonry 进行页面布局时的实例代码

1、需要在 当前视图控制器的生命周期里面,调用 AutoLayout 的方法

方法一  调用 IOS 5 以后可用的  - (void)viewWillLayoutSubviews

#pragma mark - life cycle
//这里负责把组件添加到 当前控制器容器中
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initComponents];
}

//这里负责初始化组件大小、数据
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

- (void)initComponents {
   //把所有组件添加到当前视图控制器的容器中 [self.view addSubview:self.tableView]; }
//这里进行页面自动布局代码的设置 - (void)viewWillLayoutSubviews { NSLog(@"----------里面写自动布局代码----------"); }

方法二:

#pragma mark - life cycle
//这里负责把组件添加到 当前控制器容器中
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initComponents];
}

//这里负责初始化组件大小、数据
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}
- (void)initComponents {
    [self.view addSubview:self.tableView];
    
//这里一定要调用 [self.view setNeedsUpdateConstraints]; }
//@interface UIViewController (UIConstraintBasedLayoutCoreMethods)- (void)updateViewConstraints { NSLog(@"----------updateViewConstraints----------");

//这里一定要调用 [super updateViewConstraints]; }

粘贴一段完整代码

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *halfBackgroundView;
@property (nonatomic, strong) UIView *logoView;
@property (nonatomic, strong) UIView *loginBlockView;
@property (nonatomic, strong) UIView *userNameView;
@property (nonatomic, strong) UIView *passwordView;
@property (nonatomic, strong) UIView *loginView;
@property (nonatomic, strong) UIView *forgetPWRigisterView;
@property (nonatomic, strong) UIView *copyrightView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initComponents];

}

- (void)randomBGColor:(UIView *) view{
    CGFloat r, g, b;
    r = (arc4random() % 256) / 255.0;
    g = (arc4random() % 256) / 255.0;
    b = (arc4random() % 256) / 255.0;
    view.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}

- (void)initComponents {
    [self.view addSubview:self.halfBackgroundView];
    [self.halfBackgroundView addSubview:self.logoView];
    [self.view addSubview:self.loginBlockView];
    [self.loginBlockView addSubview:self.userNameView];
    [self.loginBlockView addSubview:self.passwordView];
    [self.loginBlockView addSubview:self.loginView];
    [self.view addSubview:self.forgetPWRigisterView];
    [self.view addSubview:self.copyrightView];
    
//这里一定要调用 [self.view setNeedsUpdateConstraints]; }
//@interface UIViewController (UIConstraintBasedLayoutCoreMethods) - (void)updateViewConstraints { // 上半部分的背景 View @四边的对齐约束 [self.halfBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) { // 写法自由,left、right、top 可以分开写,也可以一起写。 make.left.right.top.equalTo(self.view); // 偏移 make.bottom.equalTo(self.view).offset(-235); }]; // logoView 设置为方块,在 halfBackgroundView 的中间 [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) { // 设置固定的大小,equalTo 需要接受 id 类型,所以需要按需转换成 NSValue 或者 NSNumber make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 100)]); // 设置中心位置 make.center.equalTo(self.halfBackgroundView); }]; // 登陆框 [self.loginBlockView mas_makeConstraints:^(MASConstraintMaker *make) { // 两个不同属性 + 使用倍乘关系 make.top.equalTo(self.halfBackgroundView.mas_bottom).multipliedBy(0.75); make.centerX.equalTo(self.halfBackgroundView); make.left.equalTo(self.view).offset(25); make.right.equalTo(self.view).offset(-25); // 设置固定值及优先级 make.height.equalTo(@170).priorityLow(); // 约束冲突时优先级低的会被覆盖掉 }]; [self.userNameView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.loginBlockView).offset(10); make.left.equalTo(self.loginBlockView).offset(15); make.right.equalTo(self.loginBlockView).offset(-15); make.height.equalTo(@35); }]; [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.userNameView); make.top.equalTo(self.userNameView.mas_bottom).offset(15); make.height.equalTo(self.userNameView); }]; [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.passwordView.mas_bottom).offset(15); make.left.equalTo(self.loginBlockView).offset(10); make.right.equalTo(self.loginBlockView).offset(-10); make.height.equalTo(@50); make.bottom.equalTo(self.loginBlockView).offset(-10); // 这里可能会和上面直接设置 loginBlockView 高度一条约束冲突,但是因为优先级的缘故并不会出错 }]; [self.copyrightView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(200, 50)]); make.centerX.equalTo(self.view); make.bottom.equalTo(self.view).offset(-10); }]; [self.forgetPWRigisterView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@30); make.left.right.equalTo(self.view); make.bottom.equalTo(self.copyrightView.mas_top).offset(-30); }];

   //这里一定要调用 [super updateViewConstraints]; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Init UI - (UIView *)halfBackgroundView { if (!_halfBackgroundView) { _halfBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; _halfBackgroundView.backgroundColor = [UIColor greenColor]; } return _halfBackgroundView; } - (UIView *)logoView { if (!_logoView) { _logoView = [[UIView alloc] initWithFrame:CGRectZero]; _logoView.backgroundColor = [UIColor lightGrayColor]; } return _logoView; } - (UIView *)loginBlockView { if (!_loginBlockView) { _loginBlockView = [[UIView alloc] initWithFrame:CGRectZero]; _loginBlockView.backgroundColor = [UIColor redColor]; } return _loginBlockView; } - (UIView *)userNameView { if (!_userNameView) { _userNameView = [[UIView alloc] initWithFrame:CGRectZero]; _userNameView.backgroundColor = [UIColor whiteColor]; } return _userNameView; } - (UIView *)passwordView { if (!_passwordView) { _passwordView = [[UIView alloc] initWithFrame:CGRectZero]; _passwordView.backgroundColor = [UIColor purpleColor]; } return _passwordView; } - (UIView *)loginView { if (!_loginView) { _loginView = [[UIView alloc] initWithFrame:CGRectZero]; _loginView.backgroundColor = [UIColor yellowColor]; } return _loginView; } - (UIView *)forgetPWRigisterView { if (!_forgetPWRigisterView) { _forgetPWRigisterView = [[UIView alloc] initWithFrame:CGRectZero]; _forgetPWRigisterView.backgroundColor = [UIColor orangeColor]; } return _forgetPWRigisterView; } - (UIView *)copyrightView { if (!_copyrightView) { _copyrightView = [[UIView alloc] initWithFrame:CGRectZero]; _copyrightView.backgroundColor = [UIColor greenColor]; } return _copyrightView; } @end
原文地址:https://www.cnblogs.com/allanliu/p/4543720.html