IOS自动布局------纯代码( NSDictionaryOfVariableBindings--addConstraints)

在做登陆的时候,如果使用storyBoard的话,会用到自动布局,可是,如果我们是用纯代码写的话,在自动布局的时候,就会比较麻烦,有时候是做判断,根据旋转的方向做判断,或者干脆不支持旋转,这里我发现用NSDictionaryOfVariableBindings这个类 也可以像用storyboard一样 实现自动布局。

先不说了 直接上代码

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *signupFormContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width,self.view.frame.size.height-100)];
    
    UITextField *emailField= [[UITextField alloc] initWithFrame:CGRectZero];
    emailField.tag = Tag_EmailTextField;
    emailField.returnKeyType = UIReturnKeyNext;
    emailField.delegate = self;
    emailField.placeholder = NSLocalizedString(@"输入邮箱", nil);
    emailField.keyboardType = UIKeyboardTypeEmailAddress;
    emailField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    emailField.layer.borderWidth = 1.f;
    emailField.layer.cornerRadius = 4.f;
    emailField.backgroundColor = [UIColor whiteColor];
    
    UITextField *usernameField = [[UITextField alloc] initWithFrame:CGRectZero];
    usernameField.tag = Tag_AccountTextField;
    usernameField.returnKeyType = UIReturnKeyNext;
    usernameField.delegate = self;
    usernameField.placeholder = NSLocalizedString(@"输入昵称(4-12个字母)", nil);
    usernameField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    usernameField.layer.borderWidth = 1.f;
    usernameField.layer.cornerRadius = 4.f;
    usernameField.backgroundColor = [UIColor whiteColor];
    
    
    UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectZero];
    passwordField.tag = Tag_TempPasswordTextField;
    passwordField.returnKeyType = UIReturnKeyDone;
    passwordField.secureTextEntry = YES;
    passwordField.delegate = self;
    passwordField.placeholder = NSLocalizedString(@"输入密码(6-16个字符,区分大小写)", nil);
    passwordField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    passwordField.layer.borderWidth = 1.f;
    passwordField.layer.cornerRadius = 4.f;
    passwordField.backgroundColor = [UIColor whiteColor];
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitButton addTarget:self action:@selector(handleSignup:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *buttonImage = [[UIImage imageNamed:@"regist_view_reg_btn_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(7.f, 7.f, 7.f, 7.f)];
    [submitButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [submitButton setTitle:NSLocalizedString(@"同意并注册", nil) forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    submitButton.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [submitButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    
    [signupFormContainer addSubview:emailField];
    [signupFormContainer addSubview:usernameField];
    [signupFormContainer addSubview:passwordField];
    [signupFormContainer addSubview:submitButton];
    [self.view addSubview:signupFormContainer];
    
    signupFormContainer.translatesAutoresizingMaskIntoConstraints = NO;
    //如果是从代码层面开始使用Autolayout,需要对使用的View的translatesAutoresizingMaskIntoConstraints的属性设置为NO.
    //即可开始通过代码添加Constraint,否则View还是会按照以往的autoresizingMask进行计算.
    emailField.translatesAutoresizingMaskIntoConstraints = NO;
    usernameField.translatesAutoresizingMaskIntoConstraints = NO;
    passwordField.translatesAutoresizingMaskIntoConstraints = NO;
    submitButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSDictionary *views = NSDictionaryOfVariableBindings(signupFormContainer, emailField, usernameField, passwordField, submitButton);
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[signupFormContainer]-20-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:views]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:signupFormContainer
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.f
                                                           constant:40]];
    NSArray *constraints = [[[[[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[emailField]-0-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views] arrayByAddingObjectsFromArray:
                               [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[usernameField]-0-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:views]] arrayByAddingObjectsFromArray:
                              [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[passwordField]-0-|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:views]] arrayByAddingObjectsFromArray:
                             [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[submitButton]-0-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views]] arrayByAddingObjectsFromArray:
                            [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailField(45)]-10-[usernameField(45)]-10-[passwordField(45)]-40-[submitButton(45)]-0-|"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:views]];

//V代表的是垂直  H代表的是横向 这里分别设置空间与边缘之间的距离  控件与控件直接的距离


 [signupFormContainer addConstraints:constraints];
    
    [self.view setNeedsUpdateConstraints];
    [self.view setNeedsLayout];
    
    // Do any additional setup after loading the view.
}


- (void)handleSignup:(id)sender
{

    NSLog(@"Click Button");
    
}



这样就实现了自动布局 就是代码比较多 但是比起判断做适配 方便多了


这里是storyboard的自动布局 不错的文章

http://conanmthu.github.io/2014/12/05/AutoLayout/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值