How do I resize views when keyboard pops up using Auto Layout
up vote
7
down vote
favorite
I have a view (a UITableView in this case, but that's not important) on my UIViewController that I want to have the height resized when the keyboard pops up.
What is the best way to do this in Auto Layout? Currently on the view I have these constraints:
- Top space to superview: 0
- Trailing space to superview: 0
- Leading space to superview: 0
- Bottom space to superview: 0
Height equals = 424
I think the quickest way of doing this, is to remove the height and bottom space constraint and just resize the actual view in the code when keyboardDidAppear notification is called, but is there any other ways to do this?
EDIT: I removed the height constraint, my bad.
ios uitableview interface-builder autolayout
add a comment |
up vote
7
down vote
favorite
I have a view (a UITableView in this case, but that's not important) on my UIViewController that I want to have the height resized when the keyboard pops up.
What is the best way to do this in Auto Layout? Currently on the view I have these constraints:
- Top space to superview: 0
- Trailing space to superview: 0
- Leading space to superview: 0
- Bottom space to superview: 0
Height equals = 424
I think the quickest way of doing this, is to remove the height and bottom space constraint and just resize the actual view in the code when keyboardDidAppear notification is called, but is there any other ways to do this?
EDIT: I removed the height constraint, my bad.
ios uitableview interface-builder autolayout
2
it may be better to update thecontentInsetproperty instead of playing with changing the actual layout in that case.
– holex
Jun 2 '14 at 8:10
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have a view (a UITableView in this case, but that's not important) on my UIViewController that I want to have the height resized when the keyboard pops up.
What is the best way to do this in Auto Layout? Currently on the view I have these constraints:
- Top space to superview: 0
- Trailing space to superview: 0
- Leading space to superview: 0
- Bottom space to superview: 0
Height equals = 424
I think the quickest way of doing this, is to remove the height and bottom space constraint and just resize the actual view in the code when keyboardDidAppear notification is called, but is there any other ways to do this?
EDIT: I removed the height constraint, my bad.
ios uitableview interface-builder autolayout
I have a view (a UITableView in this case, but that's not important) on my UIViewController that I want to have the height resized when the keyboard pops up.
What is the best way to do this in Auto Layout? Currently on the view I have these constraints:
- Top space to superview: 0
- Trailing space to superview: 0
- Leading space to superview: 0
- Bottom space to superview: 0
Height equals = 424
I think the quickest way of doing this, is to remove the height and bottom space constraint and just resize the actual view in the code when keyboardDidAppear notification is called, but is there any other ways to do this?
EDIT: I removed the height constraint, my bad.
ios uitableview interface-builder autolayout
ios uitableview interface-builder autolayout
edited Jun 2 '14 at 7:13
asked Jun 2 '14 at 6:58
Enrico Susatyo
12k1577145
12k1577145
2
it may be better to update thecontentInsetproperty instead of playing with changing the actual layout in that case.
– holex
Jun 2 '14 at 8:10
add a comment |
2
it may be better to update thecontentInsetproperty instead of playing with changing the actual layout in that case.
– holex
Jun 2 '14 at 8:10
2
2
it may be better to update the
contentInset property instead of playing with changing the actual layout in that case.– holex
Jun 2 '14 at 8:10
it may be better to update the
contentInset property instead of playing with changing the actual layout in that case.– holex
Jun 2 '14 at 8:10
add a comment |
3 Answers
3
active
oldest
votes
up vote
16
down vote
accepted
I would give you a generic idea, it may need to be re-adjusted for your actual project.
swift 4.2
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else return
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
, completion: nil)
and
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = .zero
, completion: nil)
NOTE-1: scrollView represents here any subset of UIScrollView, e.g. UITableView or UICollectionView, etc...
NOTE-2: you'll need to remove the tokens manually by invoking the removeObserver(_:) method when you are about to release the view and the closure-based observers are not necessary any longer
ObjC
I persume the UITableView *_tableView has been set up properly somewhere before.
- (void)viewDidLoad
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
completion:nil];
];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsZero];
completion:nil];
];
// ...
NOTE: if your UITableView is not at the bottom of the screen precisely, the contentInset value should be refined in this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand thecontentInsetpropery and its behaviour are designed quite well for this purpose.
– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
add a comment |
up vote
2
down vote
Keep your height constraint and connect an outlet to it:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height;
Take a look at Example 3 of this tutorial.
Update your view's height by capturing keyboard event using NSNotificationCenter:
- (void)viewWillAppear:(BOOL)animated
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void)viewWillDisappear:(BOOL)animated
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
You might want to also take a look at the accepted answer of this question for some inspirations.
So in the end, you should get something like this:
- (void)keyboardWillShow
self.CS_TableView_Height.constant = 500;//For example
[self.tableView setNeedsUpdateConstraints];
- (void)keyboardWillHide
self.CS_TableView_Height.constant = 568;// iPhone5 height
[self.tableView setNeedsUpdateConstraints];
1. the block-based notifications are much more convenient and better practice. 2. updating thecontentInsetproperty is also a much better practice.
– holex
Jun 2 '14 at 8:09
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
|
show 1 more comment
up vote
0
down vote
First off, you shouldn't add a top-bottom constraint AND a height constraint. If the screen size changes, the app will crash (unless one of the constraints have lower priority, in which case it will be removed).
Secondly, in your keyboardDidAppear notification method, you just change the bottom space to superview's constant value and call [myView setNeedsDisplay]
Edit: You don't do the setNeedsDisplay after becomeFirstResponder. You add self as observer for keyboardWillShow / keyboardWillHide notifications, and in that method, you update the constraint and call setNeedsDisplay.
Take a look at this apple post, Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field provide the code.
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I calledsetNeedsDisplayright after I calledbecomeFirstResponderto bring up the keyboard.
– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
I would give you a generic idea, it may need to be re-adjusted for your actual project.
swift 4.2
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else return
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
, completion: nil)
and
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = .zero
, completion: nil)
NOTE-1: scrollView represents here any subset of UIScrollView, e.g. UITableView or UICollectionView, etc...
NOTE-2: you'll need to remove the tokens manually by invoking the removeObserver(_:) method when you are about to release the view and the closure-based observers are not necessary any longer
ObjC
I persume the UITableView *_tableView has been set up properly somewhere before.
- (void)viewDidLoad
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
completion:nil];
];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsZero];
completion:nil];
];
// ...
NOTE: if your UITableView is not at the bottom of the screen precisely, the contentInset value should be refined in this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand thecontentInsetpropery and its behaviour are designed quite well for this purpose.
– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
add a comment |
up vote
16
down vote
accepted
I would give you a generic idea, it may need to be re-adjusted for your actual project.
swift 4.2
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else return
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
, completion: nil)
and
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = .zero
, completion: nil)
NOTE-1: scrollView represents here any subset of UIScrollView, e.g. UITableView or UICollectionView, etc...
NOTE-2: you'll need to remove the tokens manually by invoking the removeObserver(_:) method when you are about to release the view and the closure-based observers are not necessary any longer
ObjC
I persume the UITableView *_tableView has been set up properly somewhere before.
- (void)viewDidLoad
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
completion:nil];
];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsZero];
completion:nil];
];
// ...
NOTE: if your UITableView is not at the bottom of the screen precisely, the contentInset value should be refined in this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand thecontentInsetpropery and its behaviour are designed quite well for this purpose.
– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
add a comment |
up vote
16
down vote
accepted
up vote
16
down vote
accepted
I would give you a generic idea, it may need to be re-adjusted for your actual project.
swift 4.2
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else return
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
, completion: nil)
and
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = .zero
, completion: nil)
NOTE-1: scrollView represents here any subset of UIScrollView, e.g. UITableView or UICollectionView, etc...
NOTE-2: you'll need to remove the tokens manually by invoking the removeObserver(_:) method when you are about to release the view and the closure-based observers are not necessary any longer
ObjC
I persume the UITableView *_tableView has been set up properly somewhere before.
- (void)viewDidLoad
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
completion:nil];
];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsZero];
completion:nil];
];
// ...
NOTE: if your UITableView is not at the bottom of the screen precisely, the contentInset value should be refined in this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
I would give you a generic idea, it may need to be re-adjusted for your actual project.
swift 4.2
let notificationTokenKeyboardWillAppear = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) (note) in
guard let keyboardFrame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else return
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardFrame.size.height, right: 0.0)
, completion: nil)
and
let notificationTokenKeyboardWillHide = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) (_) in
UIView.animate(withDuration: CATransaction.animationDuration(), animations:
self.scrollView?.contentInset = .zero
, completion: nil)
NOTE-1: scrollView represents here any subset of UIScrollView, e.g. UITableView or UICollectionView, etc...
NOTE-2: you'll need to remove the tokens manually by invoking the removeObserver(_:) method when you are about to release the view and the closure-based observers are not necessary any longer
ObjC
I persume the UITableView *_tableView has been set up properly somewhere before.
- (void)viewDidLoad
// ...
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect _keyboardFrame = CGRectNull;
if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame];
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
completion:nil];
];
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
[UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^
[_tableView setContentInset:UIEdgeInsetsZero];
completion:nil];
];
// ...
NOTE: if your UITableView is not at the bottom of the screen precisely, the contentInset value should be refined in this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];
edited 2 days ago
answered Jun 2 '14 at 10:08
holex
21.2k75469
21.2k75469
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand thecontentInsetpropery and its behaviour are designed quite well for this purpose.
– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
add a comment |
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand thecontentInsetpropery and its behaviour are designed quite well for this purpose.
– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
Thanks a lot for your answer, I will try this out. Is it generally not a good idea to resize views that has constraints? Is it better to use contentInset when we have constraints like this?
– Enrico Susatyo
Jun 2 '14 at 10:11
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand the
contentInset propery and its behaviour are designed quite well for this purpose.– holex
Jun 2 '14 at 10:19
@EnricoSusatyo, I think that resizing the view, just because of the keyboard appearance, is too inconvenient. on the other hand the
contentInset propery and its behaviour are designed quite well for this purpose.– holex
Jun 2 '14 at 10:19
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
Thanks for your answer, it works almost perfectly. The only thing I am not quite happy with is that half of the scroll bar is still hidden behind the keyboard, but the table view content is now visible above the keyboard. However I think this is still the best solution so far.
– Enrico Susatyo
Jun 3 '14 at 1:20
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
thanks for the nice solution! starred this question.
– Nhon Nguyen
Jun 3 '14 at 7:18
3
3
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
NOTE: also set scrollIndicatorInsets so that the scrollbar shifts correctly.
– Enrico Susatyo
Jul 23 '14 at 4:19
add a comment |
up vote
2
down vote
Keep your height constraint and connect an outlet to it:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height;
Take a look at Example 3 of this tutorial.
Update your view's height by capturing keyboard event using NSNotificationCenter:
- (void)viewWillAppear:(BOOL)animated
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void)viewWillDisappear:(BOOL)animated
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
You might want to also take a look at the accepted answer of this question for some inspirations.
So in the end, you should get something like this:
- (void)keyboardWillShow
self.CS_TableView_Height.constant = 500;//For example
[self.tableView setNeedsUpdateConstraints];
- (void)keyboardWillHide
self.CS_TableView_Height.constant = 568;// iPhone5 height
[self.tableView setNeedsUpdateConstraints];
1. the block-based notifications are much more convenient and better practice. 2. updating thecontentInsetproperty is also a much better practice.
– holex
Jun 2 '14 at 8:09
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
|
show 1 more comment
up vote
2
down vote
Keep your height constraint and connect an outlet to it:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height;
Take a look at Example 3 of this tutorial.
Update your view's height by capturing keyboard event using NSNotificationCenter:
- (void)viewWillAppear:(BOOL)animated
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void)viewWillDisappear:(BOOL)animated
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
You might want to also take a look at the accepted answer of this question for some inspirations.
So in the end, you should get something like this:
- (void)keyboardWillShow
self.CS_TableView_Height.constant = 500;//For example
[self.tableView setNeedsUpdateConstraints];
- (void)keyboardWillHide
self.CS_TableView_Height.constant = 568;// iPhone5 height
[self.tableView setNeedsUpdateConstraints];
1. the block-based notifications are much more convenient and better practice. 2. updating thecontentInsetproperty is also a much better practice.
– holex
Jun 2 '14 at 8:09
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
Keep your height constraint and connect an outlet to it:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height;
Take a look at Example 3 of this tutorial.
Update your view's height by capturing keyboard event using NSNotificationCenter:
- (void)viewWillAppear:(BOOL)animated
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void)viewWillDisappear:(BOOL)animated
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
You might want to also take a look at the accepted answer of this question for some inspirations.
So in the end, you should get something like this:
- (void)keyboardWillShow
self.CS_TableView_Height.constant = 500;//For example
[self.tableView setNeedsUpdateConstraints];
- (void)keyboardWillHide
self.CS_TableView_Height.constant = 568;// iPhone5 height
[self.tableView setNeedsUpdateConstraints];
Keep your height constraint and connect an outlet to it:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height;
Take a look at Example 3 of this tutorial.
Update your view's height by capturing keyboard event using NSNotificationCenter:
- (void)viewWillAppear:(BOOL)animated
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
- (void)viewWillDisappear:(BOOL)animated
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
You might want to also take a look at the accepted answer of this question for some inspirations.
So in the end, you should get something like this:
- (void)keyboardWillShow
self.CS_TableView_Height.constant = 500;//For example
[self.tableView setNeedsUpdateConstraints];
- (void)keyboardWillHide
self.CS_TableView_Height.constant = 568;// iPhone5 height
[self.tableView setNeedsUpdateConstraints];
edited May 23 '17 at 12:18
Community♦
11
11
answered Jun 2 '14 at 7:46
Nhon Nguyen
2,15232033
2,15232033
1. the block-based notifications are much more convenient and better practice. 2. updating thecontentInsetproperty is also a much better practice.
– holex
Jun 2 '14 at 8:09
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
|
show 1 more comment
1. the block-based notifications are much more convenient and better practice. 2. updating thecontentInsetproperty is also a much better practice.
– holex
Jun 2 '14 at 8:09
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
1. the block-based notifications are much more convenient and better practice. 2. updating the
contentInset property is also a much better practice.– holex
Jun 2 '14 at 8:09
1. the block-based notifications are much more convenient and better practice. 2. updating the
contentInset property is also a much better practice.– holex
Jun 2 '14 at 8:09
1
1
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
@holex: could you please provide some code of your solution as an answer? :)
– Nhon Nguyen
Jun 2 '14 at 8:15
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
I will try this soon, but I think it's not a good idea to specify constant number as the height like this. Other keyboards like Japanese or Chinese might have different height than the English keyboard.
– Enrico Susatyo
Jun 2 '14 at 9:13
1
1
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
So you just need to get the keyboard's height and minus it dynamically
– Nhon Nguyen
Jun 2 '14 at 9:17
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
@NhonNguyen, yes, no problem, however I prefer give ideas only if I don't know the whole environment (like in this case).
– holex
Jun 2 '14 at 9:51
|
show 1 more comment
up vote
0
down vote
First off, you shouldn't add a top-bottom constraint AND a height constraint. If the screen size changes, the app will crash (unless one of the constraints have lower priority, in which case it will be removed).
Secondly, in your keyboardDidAppear notification method, you just change the bottom space to superview's constant value and call [myView setNeedsDisplay]
Edit: You don't do the setNeedsDisplay after becomeFirstResponder. You add self as observer for keyboardWillShow / keyboardWillHide notifications, and in that method, you update the constraint and call setNeedsDisplay.
Take a look at this apple post, Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field provide the code.
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I calledsetNeedsDisplayright after I calledbecomeFirstResponderto bring up the keyboard.
– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
add a comment |
up vote
0
down vote
First off, you shouldn't add a top-bottom constraint AND a height constraint. If the screen size changes, the app will crash (unless one of the constraints have lower priority, in which case it will be removed).
Secondly, in your keyboardDidAppear notification method, you just change the bottom space to superview's constant value and call [myView setNeedsDisplay]
Edit: You don't do the setNeedsDisplay after becomeFirstResponder. You add self as observer for keyboardWillShow / keyboardWillHide notifications, and in that method, you update the constraint and call setNeedsDisplay.
Take a look at this apple post, Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field provide the code.
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I calledsetNeedsDisplayright after I calledbecomeFirstResponderto bring up the keyboard.
– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
add a comment |
up vote
0
down vote
up vote
0
down vote
First off, you shouldn't add a top-bottom constraint AND a height constraint. If the screen size changes, the app will crash (unless one of the constraints have lower priority, in which case it will be removed).
Secondly, in your keyboardDidAppear notification method, you just change the bottom space to superview's constant value and call [myView setNeedsDisplay]
Edit: You don't do the setNeedsDisplay after becomeFirstResponder. You add self as observer for keyboardWillShow / keyboardWillHide notifications, and in that method, you update the constraint and call setNeedsDisplay.
Take a look at this apple post, Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field provide the code.
First off, you shouldn't add a top-bottom constraint AND a height constraint. If the screen size changes, the app will crash (unless one of the constraints have lower priority, in which case it will be removed).
Secondly, in your keyboardDidAppear notification method, you just change the bottom space to superview's constant value and call [myView setNeedsDisplay]
Edit: You don't do the setNeedsDisplay after becomeFirstResponder. You add self as observer for keyboardWillShow / keyboardWillHide notifications, and in that method, you update the constraint and call setNeedsDisplay.
Take a look at this apple post, Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field provide the code.
edited Jun 2 '14 at 7:26
answered Jun 2 '14 at 7:03
Lord Zsolt
4,71083261
4,71083261
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I calledsetNeedsDisplayright after I calledbecomeFirstResponderto bring up the keyboard.
– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
add a comment |
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I calledsetNeedsDisplayright after I calledbecomeFirstResponderto bring up the keyboard.
– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I called
setNeedsDisplay right after I called becomeFirstResponder to bring up the keyboard.– Enrico Susatyo
Jun 2 '14 at 7:14
Hey, thanks Lord Zsolt, but It seems like the view still does not resize after I called that. I've removed the height constraint too. I called
setNeedsDisplay right after I called becomeFirstResponder to bring up the keyboard.– Enrico Susatyo
Jun 2 '14 at 7:14
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
Take a look at the edit.
– Lord Zsolt
Jun 2 '14 at 7:29
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23988866%2fhow-do-i-resize-views-when-keyboard-pops-up-using-auto-layout%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
it may be better to update the
contentInsetproperty instead of playing with changing the actual layout in that case.– holex
Jun 2 '14 at 8:10