ios-屏幕适配masonry的使用

88 篇文章 0 订阅

在我们项目开发的过程 屏幕适配很重要 

下面这个例子适配 各种iPhone 屏幕 (iPhone4,iPhone4s,iPhone5,iPhone5s,iPhone6,iPhone6s)

原理很简单 知道他的使用方法即可

我们已经过了4.0 屏幕的时代 苹果推出一款款新的产品之后

我们这些攻城狮也必须适应新的产品  

6.0之后 推出了AutoLayout 技术  但是使用的人不是很多  

我们在使用xib的过程中虽然屏幕适配比较方便 可以视话 

减少了代码的量 但是我们维护起来成本是很大的

今天介绍一种现在主流的 技术 masonry的使用 

这个用纯代码实现 我们想要对控件的约束 我们可以根据自己的需求进行设置

这个使用起来比较简单 并且适配各种屏幕 

我们只需要 分清楚上下左右 顶部 底部  和参考的相对控件 即可

代码实现一个 简单的登录页面 为了帮助新手学习 

简单的搭建了一个页面 用block 设置回调

简单的封装了一个视图

代码如下:

#import <UIKit/UIKit.h>

typedef void(^LoginBtnClickBlock)(NSString *userName,NSString *userPasswd);

@interface LoginView : UIView

+ (id)loginView;


- (void)setLoginBtnClickBlock:(LoginBtnClickBlock)loginBtnClick;

@end

#import "LoginView.h"
#import "Masonry.h"


@interface LoginView()

{
    LoginBtnClickBlock _loginBtnClickBlock;

}

@property (nonatomic, weak) UILabel *userLabel;

@property (nonatomic, weak) UILabel *passedLabel;

/**
 *  用户名
 */
@property (nonatomic, weak) UITextField *userTextField;

/**
 *  密码
 */
@property (nonatomic, weak) UITextField *passwdTextField;

/**
 *  登录按钮
 */
@property (nonatomic, weak) UIButton *btnClick;


@end



@implementation LoginView

+ (id)loginView
{
    return  [[self alloc]init];
}

#pragma mark - getter方法

- (UILabel *)userLabel
{
    if (!_userLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"用户名:";
        [self addSubview:label];
        label.font = [UIFont systemFontOfSize:15];
//        label.backgroundColor = [UIColor redColor];
        _userLabel = label;
    }
    return _userLabel;
}


- (UILabel *)passedLabel
{
    if (!_passedLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"密码:";
        label.font = [UIFont systemFontOfSize:15];
//        label.backgroundColor = [UIColor redColor];
        [self addSubview:label];
        _passedLabel = label;
    }
    return _passedLabel;
}


- (UITextField *)userTextField
{
    if (!_userTextField) {
        UITextField *userTextField = [[UITextField alloc]init];
        userTextField.placeholder = @"请输入用户名";
        userTextField.font = [UIFont systemFontOfSize:15];
        userTextField.borderStyle = UITextBorderStyleRoundedRect;
        [self addSubview:userTextField];
        _userTextField = userTextField;
    }
    return _userTextField;
}


- (UITextField *)passwdTextField
{
    if (!_passwdTextField) {
        UITextField *passwdTextField = [[UITextField alloc]init];
        passwdTextField.placeholder = @"请输入密码";
        passwdTextField.secureTextEntry = YES;
        passwdTextField.font = [UIFont systemFontOfSize:15];
        passwdTextField.borderStyle = UITextBorderStyleRoundedRect;
        [self addSubview:passwdTextField];
        _passwdTextField = passwdTextField;
    }
    return _passwdTextField;
}

- (UIButton *)btnClick
{
    if (!_btnClick) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"登陆" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btn.layer setCornerRadius:10];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        _btnClick = btn;
    }
    return _btnClick;
}

#pragma mark - setter方法
- (void)setLoginBtnClickBlock:(LoginBtnClickBlock)loginBtnClick
{
    //赋值
    _loginBtnClickBlock = loginBtnClick;
}

#pragma mark - 点击事件 
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"点击");
    
    //设置回调
    if (_loginBtnClickBlock) {
        _loginBtnClickBlock(self.userTextField.text,self.passwdTextField.text);
    }
}



#pragma mark - 设置frame 

- (void)layoutSubviews
{
    /**
     *  先调用父类方法
     */
    [super layoutSubviews];
    
    /**
        设置约束条件
     */
    [self.userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(100);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    [self.userTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userLabel.mas_right).offset(10);
        make.top.mas_equalTo(self.userLabel.mas_top);
//        make.width.mas_equalTo(200);
        make.rightMargin.mas_equalTo(-30);
        make.height.mas_equalTo(30);
    }];
    
    [self.passedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userLabel.mas_left);
        make.top.mas_equalTo(self.userLabel.mas_bottom).offset(30);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    [self.passwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.passedLabel.mas_right).offset(10);
        make.top.mas_equalTo(self.passedLabel.mas_top);
        make.rightMargin.mas_equalTo(-30);
        make.height.mas_equalTo(30);
    }];
    
    [self.btnClick mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.top.mas_equalTo(self.passwdTextField.mas_bottom).offset(20);
        make.height.mas_equalTo(30);
        make.rightMargin.mas_equalTo(-50);
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

#import "ViewController.h"
#import "LoginView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"登陆";
    
    [self createView];
  
}

#warning 写成了加方法 出错
- (void)createView
{
    LoginView *login = [LoginView loginView];
    login.frame = self.view.frame;
    [self.view addSubview:login];
    /**
        //设置回调 获取username 和passwd
     */
    [login setLoginBtnClickBlock:^(NSString *userName, NSString *userPasswd) {
        NSLog(@"username----%@",userName);
        NSLog(@"passwd------%@",userPasswd);
        [self alertView];
    }];
    
}


- (void)alertView
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值