gpt4 book ai didi

ios - 如何使用自动布局以编程方式修复所有 iPhone 英寸中的文本字段和按钮

转载 作者:行者123 更新时间:2023-11-30 14:13:25 26 4
gpt4 key购买 nike

嗨,我是 iOS 初学者,在我的项目中,我必须以编程方式应用自动布局,但这里所有字段宽度并不适合 iPhone 4@5 英寸中的所有 iPhone 英寸,没关系,但 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

最佳答案

对于“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];

结果:

关于ios - 如何使用自动布局以编程方式修复所有 iPhone 英寸中的文本字段和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31501097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com