I'm implementing a form inside a UIScrollView
. I'm trying to add some space at the bottom of the content of a scroll view when the keyboard is opened so the user can see all the fields. I put the form view inside the UISCrollView
adding all the needed constraints with the following code:
[_infoView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView(1730)]" options:0 metrics:nil views:views]];
[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_infoView]|" options:0 metrics:nil views:views]];
[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_infoView]|" options:0 metrics:nil views:views]];
[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_infoView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:_scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
As you can see, I specify in the first line the height of the form and the scrollview
adapts its content size automatically. Now I want to increase the height of the form, so I've tried to reset the constraint for the height with a greater one but it won't work. Then I've tried to use the [_scrollView setContentSize:]
method but this also won't work. Can anyone help me please?
UIScrollView has gone through some major improvements. Starting with iOS 11, Apple introduced content and frame layout guides to make it a little less confusing to configure the content inside of your scroll view.
These constraints tell the UIScrollView the boundaries of your content (sets the contentSize of the UIScrollView ). You usually only want your content to scroll in one direction. In my case, I want the scroll view to scroll vertically. Therefore, I need to set the width of my single content view to be the width of the scroll view.
Without any controls inside the content view, or if there are no placeholder width and height constraints on the content view, the scroll view cannot determine the content size. Ignore the errors for now.
Manually resize the View element with your cursor so that it fills the width of the screen. Click on the View element in the object hierarchy and drag+release your cursor onto the Scroll View element above it in the hierarchy. Click on the 4 topmost options to apply these constraints.
If I understand the problem, I would recommend adjusting the contentInset
property on the UIScrollView
rather than call layoutSubviews
. Note that the docs say:
Use this property to add to the scrolling area around your content. The unit of size is points. The default value is
UIEdgeInsetsZero
.
You still have to listen for the keyboard hide/show notifications so as to know when to adjust the height of your scrollview:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Then in keyboardWillShow
you can do this:
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 100, 0);
scrollView.contentInset = insets;
100
is the height by which you want to adjust your scrollView
. I do this with a UITableView
in which I have form elements as UITableViewCell
s and it works well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With