Reputation: 375
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?
Upvotes: 9
Views: 4008
Reputation: 7145
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.
Upvotes: 4
Reputation: 1570
I am not sure where you add the above code, but the below should solve your problem
In Your init Function, add the below:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
Add the below to your .h
CGSize keyboardSize;
int keyboardHidden; // 0 @ initialization, 1 if shown, 2 if hidden
Add the below to your .m
-(void) noticeShowKeyboard:(NSNotification *)inNotification {
keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardHidden = 1;
[self layoutSubviews]; // Not sure if it is called automatically, so I called it
}
-(void) noticeHideKeyboard:(NSNotification *)inNotification {
keyboardHidden = 2;
[self layoutSubviews]; // Not sure if it is called automatically, so I called it
}
- (void) layoutSubviews
{
[super layoutSubviews];
if(keyboardHidden == 1) {
scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height + keyboardSize.height);
}
else if(keyboardHidden == 2) {
scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height - keyboardSize.height);
}
}
I overrided layoutsubviews
, now I think it should work.
Upvotes: -2