如何使用autolayouts

时间:2015-07-19 12:16:59

标签: ios swift

嗨,我是iOS的初学者,在我的项目中,我必须以编程方式应用自动布局,但是这里所有的字段宽度都不适合iPhone 4 @ 5英寸的所有iPhone英寸都可以,但我的手机6英寸宽度不是适合所有领域,如4 @ 5,并根据我的代码屏幕来像第一个图像,但我想修复所有字段像第二屏幕我的意思是所有字段必须适合所有我的手机英寸请帮助我一个人我的代码低于enter image description here

myView = [UIview new];
        myview .translatesAutoresizingMaskIntoConstraints = NO;
        myview .backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view myView ];


textfield1= [UITextField new];
        textfield1.translatesAutoresizingMaskIntoConstraints = NO;
        textfield1.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view textfield1];


     textfield2= [UITextField new];
        textfield2.translatesAutoresizingMaskIntoConstraints = NO;
        textfield2.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view textfield2];

  textfield3= [UITextField new];
        textfield3.translatesAutoresizingMaskIntoConstraints = NO;
        textfield3.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view textfield3];


 pickbutton = [UIButton new];
        pickbutton .translatesAutoresizingMaskIntoConstraints = NO;
        pickbutton .backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view pickbutton];




 Submit= [UIButton new];
        Submit.translatesAutoresizingMaskIntoConstraints = NO;
        Submit.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view Submit];


     Clear= [Clearnew];
        Clear.translatesAutoresizingMaskIntoConstraints = NO;
        Clear.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];
        [self.view Clear];


//Applying autolayouts for MyView

        NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint];

        constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint];

        constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
        [self.view addConstraint:constraint];

        constraint1 = [NSLayoutConstraint constraintWithItem:myView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:460.0f];
        [self.view addConstraint:constraint];


        //Applying autolayouts for textfield1
        NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:textfield1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint1];

        constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint1];

        constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
        [self.view addConstraint:constraint1];

        constraint1 = [NSLayoutConstraint constraintWithItem:textField1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint1];


     //Applying autolayouts for textfield2
        NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:textfield2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView  attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:60.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint2];


  NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:textfield2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView  attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:60.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
        [self.view addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:textField2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint2];

//Applying auto layouts for textfield3

  NSLayoutConstraint * constraint11 = [NSLayoutConstraint constraintWithItem:textfield3 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView  attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
        [self.view addConstraint:constraint11];

        constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f];
        [self.view addConstraint:constraint11];

        constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300.0f];
        [self.view addConstraint:constraint11];

        constraint11 = [NSLayoutConstraint constraintWithItem:textField3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint11];


     //Applying autolayouts for pickerbutton
        NSLayoutConstraint * constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
        [self.view addConstraint:constraint22];

        constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f];
        [self.view addConstraint:constraint22];

        constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:80.0f];
        [self.view addConstraint:constraint22];

        constraint22 = [NSLayoutConstraint constraintWithItem:pickerbutton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint22];


     //Applying autolayouts for clearButton
        NSLayoutConstraint * constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:207.0f];
        [self.view addConstraint:constraint4];

        constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myView attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
        [self.view addConstraint:constraint4];

        constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:133.0f];
        [self.view addConstraint:constraint4];

        constraint4 = [NSLayoutConstraint constraintWithItem:Clear attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: myView attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
        [self.view addConstraint:constraint4];

enter image description here

1 个答案:

答案 0 :(得分:0)

使用“myView”时,不应设置绝对大小。
以下是“myView”的示例:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view setBackgroundColor:[UIColor darkGrayColor]];
myView.translatesAutoresizingMaskIntoConstraints = NO;
[myView setBackgroundColor:[UIColor colorWithRed:74.0/255.0 green:166.0/255.0 blue:224.0/255.0 alpha:1]];
[self.view addSubview:myView];
NSDictionary *viewDic = @{@"myView": myView};
NSDictionary *metrics = @{@"vSpacing":@10, @"hSpacing":@10};
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[myView]-vSpacing-|"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewDic];
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[myView]-hSpacing-|"
                                                                            options:0
                                                                            metrics:metrics
                                                                              views:viewDic];
[self.view addConstraints:constraint_POS_V];
[self.view addConstraints:constraint_POS_H];

结果: