如何在 ios 中使用自动布局更改滚动视图内容大小

Posted

技术标签:

【中文标题】如何在 ios 中使用自动布局更改滚动视图内容大小【英文标题】:How to change scrollView content sizes using auto-layouts in ios 【发布时间】:2015-10-20 06:28:56 【问题描述】:

我在我的滚动视图上添加 3 个 UITextfields 和 1 个 UIButton。

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

当我点击键盘上的返回按钮时,滚动必须默认滚动,使用自动布局为滚动视图 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;

【问题讨论】:

how to set scrollview content size with auto layout?的可能重复 我的问题完全不同,即当我单击 textfileds 滚动时必须滚动到某个内容大小,并且当我单击键盘滚动中的返回按钮到达它的先前位置时 【参考方案1】:

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

**

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

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

【讨论】:

【参考方案2】:

keyBoards 出现时。

   CGRect frame = currentActiveView.frame; // your scrollview frame 
frame.size.height = SCREEN_HEIGHT - 216 - keyboardAccesssory.frame.size.height;
currentActiveView.frame = frame;

[yourScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];

当键盘退出时..

CGRect frame = currentActiveView.frame; // your scrollview frame 
frame.size.height = SCREEN_HEIGHT;

currentActiveView.frame = frame;

[yourScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];

【讨论】:

【参考方案3】:

您需要使用视图的框架。 显示键盘时移动键盘上方的框架,而隐藏键盘时重置其位置。 您需要以编程方式进行。 您还可以阅读 Apple 的编程指南。 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW3

【讨论】:

以上是关于如何在 ios 中使用自动布局更改滚动视图内容大小的主要内容,如果未能解决你的问题,请参考以下文章

iOS自动布局与滚动视图:内容大小问题

如何使用自动布局设置滚动视图内容大小?

Xamarin iOS:滚动视图中的自动布局?

如何使用自动布局 beta 7 UIKIT 修复 xcode 11 中可滚动内容大小的歧义

iOS:自动布局的问题。模拟器结果更改为故事板更改

如何使用自动调整大小和自动布局并更改其默认行为? [关闭]