gpt4 book ai didi

ios - 如何在 ios 中使用自动布局更改 ScrollView 内容大小

转载 作者:行者123 更新时间:2023-12-01 16:29:01 25 4
gpt4 key购买 nike

我在 ScrollView 上添加 3 个 UITextfields 和 1 个 UIButton。

我的主要要求是当我单击 UITextfield 滚动时,必须向上滚动到键盘上方用户可见的所有字段。

当我单击键盘滚动上的返回按钮时,默认情况下必须滚动使用自动布局为 ScrollView contentSize 设置的内容。

我的代码:

@interface ViewController10 ()
{
UIScrollView * scrollView;
UITextField * emailTextField;
UITextField * nameTextField;
UITextField * passwword;
UIButton * submit;
}

@end

@implementation ViewController10

- (void)viewDidLoad {
[super viewDidLoad];

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];

NSDictionary * 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]];
}

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[emailTextField(30)]-130-[nameTextField(30)]-130-[passwword(30)]-60-[submit(30)]-20-|"
options:0
metrics:nil
views:viewsDic]];

}

-(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];

return YES;
}

最佳答案

请查看这个很棒的教程,它对您绝对有帮助。

**

Using UIScrollView with Auto Layout in iOS



**

正如博客中所说,当您使用自动布局时,您需要以这种方式设置约束。

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeading
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
[self.view addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeTrailing
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0];
[self.view addConstraint:rightConstraint];


根据您的要求更改值和名称。

关于ios - 如何在 ios 中使用自动布局更改 ScrollView 内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229489/

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