Masonry 简单布局

简单布局

Masonry 简单布局_第1张图片
Paste_Image.png

上代码


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"Masonry练习";
     __weak typeof(self) WeakSelf = self;
    
    
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    
//    mas_equalTo和equalTo区别:前者比后者多了类型转换操作,支持CGSize CGPoint NSNumber UIEdgeinsets。mas_equalTo是equalTo的封装,equalTo适用于基本数据类型,而mas_equaalTo适用于类似UIEdgeInsetsMake 等复杂类型,基本上它可以替换equalTo    
        //水平居中
        make.centerX.mas_equalTo(WeakSelf.view);
        //距上80
        make.top.mas_equalTo(@80);
        //宽 80
        make.width.mas_equalTo(@80);
        //高 80
        make.height.mas_equalTo(@80);

    }];
    // 回到主线程  因为block 是异步的  切圆需要 得知图片的宽高
    dispatch_async(dispatch_get_main_queue(), ^{

        //圆角效果  动画效果没有切角失效
        _photoImageView.layer.masksToBounds = YES;
        //高的一半切成圆
        _photoImageView.layer.cornerRadius = _photoImageView.frame.size.height/2;
        
    });
    
    
    [self.userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
       
        //距 头像  30
        make.top.mas_equalTo(WeakSelf.photoImageView.mas_bottom).offset(30);
        make.left.mas_equalTo(30);
        // 屏幕宽 - 60
        make.width.mas_equalTo(selfWidth - 60);
        make.height.mas_equalTo(40);
    }];
    
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //距上
        make.top.mas_equalTo(WeakSelf.userNameTF.mas_bottom).offset(30);
        make.left.mas_equalTo(30);
        // 屏幕宽 - 60
        make.width.mas_equalTo(selfWidth - 60);
        make.height.mas_equalTo(40);
    }];
    
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //距上
        make.top.mas_equalTo(WeakSelf.passwordTF.mas_bottom).offset(30);
        make.left.mas_equalTo(30);
        // 屏幕宽 - 60
        make.width.mas_equalTo(selfWidth - 60);
        make.height.mas_equalTo(@40);
    }];
    

    
    [self.forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //距上
        make.top.mas_equalTo(WeakSelf.loginButton.mas_bottom).offset(20);
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //距上
        make.top.mas_equalTo(WeakSelf.loginButton.mas_bottom).offset(20);
        // 距离右面
        make.right.mas_equalTo(-30);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
    
}

- (UIImageView *)photoImageView{
    if (_photoImageView == nil){
        _photoImageView = [[UIImageView alloc]init];
        //圆角效果  动画效果没有切角失效
        _photoImageView.layer.masksToBounds = YES;
        
        
        //设置边框颜色
        _photoImageView.layer.borderColor = [UIColor greenColor].CGColor;
        
        //设置边框宽度
        _photoImageView.layer.borderWidth = 4;
        //设置边界外内容不显示
        _photoImageView.clipsToBounds = YES;
        
        _photoImageView.backgroundColor = [UIColor redColor];
        
        [self.view addSubview:_photoImageView];
    }
    return _photoImageView;
}

// 用户名
- (UITextField *)userNameTF{
    if (_userNameTF == nil) {
        _userNameTF = [[UITextField alloc]init];
        _userNameTF.placeholder = @"请输入用户名";
        _userNameTF.font = [UIFont systemFontOfSize:15];
        _userNameTF.textAlignment = NSTextAlignmentCenter;
        _userNameTF.layer.borderWidth = 1;
        [self.view addSubview:_userNameTF];
    }
    return _userNameTF;
}

// 密码
- (UITextField *)passwordTF{
    if (_passwordTF == nil){
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.font = [UIFont systemFontOfSize:15];
        _passwordTF.textAlignment = NSTextAlignmentCenter;
        _passwordTF.layer.borderWidth = 1;
        [self.view addSubview:_passwordTF];
    }
    return _passwordTF;
}

// 登录 button
- (UIButton *)loginButton{
    if (_loginButton == nil){
      
       _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
       _loginButton.backgroundColor = [UIColor whiteColor];
        
        _loginButton.layer.borderWidth = 1;
        _loginButton.layer.borderColor = [UIColor blackColor].CGColor;
        
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        
        [_loginButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}

- (UIButton *)forgetPasswordButton{
    if (_forgetPasswordButton == nil){
        _forgetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _forgetPasswordButton.backgroundColor = [UIColor whiteColor];
        
        [_forgetPasswordButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        _forgetPasswordButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
        
        [_forgetPasswordButton addTarget:self action:@selector(forgetPasswordButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _forgetPasswordButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;;
        
        [self.view addSubview:_forgetPasswordButton];
        
    }
    return _forgetPasswordButton;
}

- (UIButton *)registerButton{
    if (_registerButton == nil){
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _registerButton.backgroundColor = [UIColor whiteColor];
        
        [_registerButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;;
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        
        [_registerButton addTarget:self action:@selector(registerButtonAction) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_registerButton];
        
    }
    return _registerButton;
}

//@property (nonatomic,strong)UIButton *registerButton;

//登录点击事件
- (void)loginButtonAction{
    
    
}

//忘记密码点击事件
- (void)forgetPasswordButtonAction{
    
    
}

// 注册点击事件
- (void)registerButtonAction{
    
    
}

你可能感兴趣的:(Masonry 简单布局)