嗨,我是自动布局的初学者,我正在插入" 3" textfields和" 1"滚动视图上的按钮
这里我的要求是根据i-phone inches" Top space"必须是调整,当我点击文本字段时,所有字段必须在键盘上滚动,当我点击"返回"键盘上的按钮然后滚动必须像以前一样滚动
为此,我尝试了下面的代码,但这是太长的代码可以任何人使用短过程解释我这个概念
#import "ViewController10.h"
@interface ViewController10 ()
{
UIScrollView * scrollView;
UITextField * emailTextField;
UITextField * nameTextField;
UITextField * passwword;
UIButton * submit;
NSDictionary * viewsDic;
NSArray * verticalConstraints;
int height;
}
@end
@implementation ViewController10
- (void)viewDidLoad {
[super viewDidLoad];
height = [UIScreen mainScreen].bounds.size.height;
scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
emailTextField = [self createLabelWithText];
emailTextField.delegate = self;
[scrollView addSubview: emailTextField];
nameTextField = [self createLabelWithText];
nameTextField.delegate = self;
[scrollView addSubview: nameTextField];
passwword = [self createLabelWithText];
passwword.delegate = self;
[scrollView addSubview: passwword];
submit = [[UIButton alloc]init];
submit.backgroundColor = [UIColor orangeColor];
[submit setTitle: @"Submit" forState: UIControlStateNormal];
submit.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:submit];
viewsDic = NSDictionaryOfVariableBindings(scrollView,emailTextField,nameTextField,passwword,submit);
//Applying autolayouts for scrolview
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[scrollView]-0-|"]
options:0
metrics:nil
views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[scrollView]-0-|"]
options:0
metrics:nil
views:viewsDic]];
//Applying autolayouts for textfields and button
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:emailTextField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
NSArray * keys = @[@"emailTextField",@"nameTextField",@"passwword",@"submit"];
for (NSString * key in keys) {
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-10-[%@]-10-|",key]
options:0
metrics:nil
views:viewsDic]];
}
if (height == 480.0) {
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if (height == 568.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if(height == 667.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
[scrollView addConstraints:verticalConstraints];
}
-(UITextField *)createLabelWithText{
UITextField * textfield = [[UITextField alloc] init];
textfield.textColor = [UIColor whiteColor];
textfield.backgroundColor = [UIColor lightGrayColor];
textfield.translatesAutoresizingMaskIntoConstraints = NO;
return textfield;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
scrollView.contentSize = CGSizeMake(320, 700);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
[scrollView removeConstraints:verticalConstraints];
if (height == 480.0) {
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if (height == 568.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
else if(height == 667.0){
verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic];
}
[scrollView addConstraints:verticalConstraints];
return YES;
}
@end