Reputation: 8158
I have a UITextView
(which is also a UIScrollView
) which contains a bunch of text. In the screenshot there is more text underneath the keyboard. I can't scroll up to see that text - no matter what I do that text remains underneath the keyboard.
How can I fix things so that I can scroll to see all the text?
Upvotes: 1
Views: 197
Reputation: 76
This works and it's pretty simple.
In .h
@property (weak, nonatomic) IBOutlet UITextView *tv;
@property CGSize keyboardSize;
In .m
- (void)viewDidLoad {
[super viewDidLoad];
// 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) keyboardWillShow: (NSNotification*) aNotification {
// Get the keyboard size from the notification userInfo
NSDictionary *info = [aNotification userInfo];
self.keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Adjust the content inset from the bottom by the keyboard's height
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, self.keyboardSize.height, 0);
self.tv.contentInset = contentInsets;
}
- (void) keyboardWillHide: (NSNotification*) aNotification {
// Reset the content inset when the keyboard is dismissed
self.tv.contentInset = UIEdgeInsetsZero;
}
Upvotes: 3
Reputation: 2168
To avoid all the manual resizing and stuff, I recommend using this great library - https://github.com/hackiftekhar/IQKeyboardManager. It will do all the hard work for you.
Upvotes: 1
Reputation: 2251
The scrollView
has a property called contentSize
which determines the area up to which the user can scroll. You would have to manually change this value to compensate the extra scroll space due to the keyboard.
What I suggest is to register for the notifications UIKeyboardWillHideNotification
UIKeyboardWillShowNotification
.
When the keyboard is about to show, the UIKeyboardWillShowNotification
notification is fired and in the corresponding method add the keyboard height to the scroll contentSize
height.
Similarly, deduct this height fom the scroll contentSize
height in the UIKeyboardWillHideNotification
notification.
Hope this helps! :)
Upvotes: 1