I set scroll view offset to show text field hidden by keyboard. If the user scrolls while keyboard is show, scroll view snaps back down - ios

As the title says, I have a UITextField inside a UIScrollView. When the keyboard is shown, I adjust the contentOffset of the scroll view so that the text field is hidden. The issue is if the text field is at the bottom of the scroll view. When the keyboard pops up, the scroll view adjusts as needed. But, if the user touches and scrolls the area above the keyboard, then the scroll view snaps back down. Intuitively, this makes sense because I've programatically over-scrolled the scroll view, but from a user perspective it is not nice.
What can I do about this? One thing I've thought of is to move the entire scroll view frame instead of setting the content offset. I don't know how to do this. I have the desired change in offset stored in a CGFloat. Can someone help?

You need to change the contentInset. The contentOffset is the current scroll position so when the user scrolls it gets reset.
An example of this can be found here: https://stackoverflow.com/a/16806736/78496

One thing you could do is listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification system notifications to know when to modify the contentInset of your UIScrollView. You could do this at the viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
Don't forget to remove yourself as an observer too,
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
When the keyboard will show or hide you can adjust the contentInset given the keyboard's height.
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardEndFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIEdgeInsets scrollInsetWithKeyboard = UIEdgeInsetsMake(0, 0, 0, -keyboardEndFrame.height, 0)
self.scrollView.contentInset = scrollInsetWithKeyboard; // If you have a custom inset maybe now would be a good idea to save it so you can restore it later
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.scrollView.contentInset = UIEdgeInsetsZero; // Or to whatever inset you had before
}
When those two methods are fired you could also animate the contentOffset if you'd like.

You should use this library : https://github.com/hackiftekhar/IQKeyboardManager
It is really awesome, you have only to add this lib in your project and it will manage all your textfields. You have zero line of code to do to implement this lib, it is automatic. I use it in all my project and it works fine everywhere (for textfield in a cell, tableview, scrollview...)

Related

How to detect keyboard frame changes when dismiss the keyboard interactively?

Consider this scenario, I have a textview with keyboard Dismiss interactively set in storyboard, so when user scroll down and able to dismiss keyboard interactively.
I have constraints on the textview to bottom to make sure it is always fully displayed on the view.
Current problem is, when user gradually scroll down to dismiss the keyboard, I can not detect the keyboard frame changes. I tried UIKeyboardWillHideNotification and UIKeyboardWillChangeFrameNotification, they were only called after the keyboard dismissed.
So my question is, how can we detect keyboard frame changes simultaneously when dismiss the keyboard interactively?
If you want to observe keyboard frame changes even when the keyboard is being dragged you can use this: https://github.com/brynbodayle/BABFrameObservingInputAccessoryView
Basically you create a placeholder input view for keyboard (which sticks to the keyboard all the time, even while dragging) and you observe it's frame changes. Those changes are being returned in a block, so you get current frame of the keyboard all the time.
You shouldn't change the textView height to fit all view. Instead - you should change contentInset field so your textView will stay the same height and you won't have to bother about tracking frame of the interactive keyboard.
See answer here:
How do I scroll the UIScrollView when the keyboard appears?
In your viewDidLoad method add these line:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
the add these methods to your viewController
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.30
delay:0.0
options:(7 << 16) // This curve ignores durations
animations:^{
self.buttonsBottomConstraint.constant = keyboardSize.height - self.footerView.bounds.size.height + 4.0;
[self.view layoutIfNeeded];
}
completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.30
delay:0.0
options:(7 << 16) // This curve ignores durations
animations:^{
self.buttonsBottomConstraint.constant = 0.0;
[self.view layoutIfNeeded];
}
completion:nil];
}

Objective-C: detect keyboard size/origin *before* it opens?

How is this done? I'm looking for iOS7/8 solutions. keyboardWillShow is not satisfactory because I need to resize a view based on the keyboard height before the keyboard actually shows.
keyboardWillShow is fired before the keyboard is shown. If that's not satisfactory for you, then you'll need to be smart about the keyboard size.
If the keyboard has never been shown on screen in your app before, you can make an educated guess by first checking for the device type and orientation and then having a quick lookup table of the default keyboard sizes. This will cover you 99% of the time.
In the event that the user has a custom keyboard in use that is not a standard size, you can use the keyboard size from keyboardWillShow, store it and the orientation (NSUserDefaults would work well here) and then reference the stored value the next time you need the size.
This wouldn't cover your needs every time because you wouldn't know which keyboard is going to be pulled up until keyboardWillShow is called. For example, you could replace the inputView on two different UITextField's with your own custom views; those views could be different sizes. You wouldn't know which one was going to be shown until keyboardWillShow would be called.
EDIT
There is another possibility...if you know the view that you want to show the keyboard for explicitly.
I added this to viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShowFirstTimeNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[self.view addSubview:self.textField];
[self.textField becomeFirstResponder];
Then, add a method for handling that notification. This method should only be called once and then inside of it remove the notification so it's never called again.
- (void)keyboardWillShowFirstTimeNotification:(NSNotification*)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
NSLog(#"keyboardFrameBeginRectHeight: %f", keyboardFrameBeginRect.size.height);
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[self.textField resignFirstResponder];
}
This will log the keyboard height without ever showing it on screen.
If you wanted to extend this further, you could subclass UITextField and UITextView to have properties for keyboard height for different orientations and then you could store that value directly in the text fields and text views. Then, you'd be able to have multiple input view sizes and know what they will be prior to showing them.
Currently the time for the keyboard to show is 0.3 seconds, but Apple may change that at any time. This is also true for the keyboard size. The default keyboard in portait mode is 216px in height and in landscape it is 162px, but that may also change at any time. If (for any reason) you need to find out the keyboard size you can do that pretty easily.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Read the userInfo for the key UIKeyboardFrameBeginUserInfoKey
-(void)keyboardWillShow:(NSNotification*)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
NSLog(#"%#", NSStringFromCGRect(keyboardFrameBeginRect));
}

UIScrollView with keyboard visible

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?
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;
}
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! :)
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.

UITextView top margin

I'm using a textview and noticed that iOS 7 leaves a top margin by default. See image in the following
I read different posts in which the most common solution is to use:
[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
But those insets are just a custom solution for a particular device, textview, font size, and so on. Therefore, there are no specific insets applicable to any solution... even worst, I would have to programmatically define different insets to account for all iOS devices and orientations.
Good news is that I found that whenever the textview becomes the first responder and keyboard is shown on screen, this top margin disappears even after keyboard has gone. By the way, I'm resizing contentInset on UIKeyboardDidShowNotification and UIKeyboardWillHideNotification.
See image when keyboard did show:
See image when keyboard has gone:
Is there a way to simulate keyboard show and hide? So that content inset disappears as explain above.
I have already tried making textview become first responder and then resign it, but for this approach the user would have to see the whole keyboard show-hide animation.
Thanks in advance!
My code below:
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
if(self.topMarginIsAlreadyResized == NO) {
[self.myTextView becomeFirstResponder]; // Keyboard will show to eliminate top margin when view appears
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)handleKeyboardDidShow:(NSNotification *)notification {
if(self.topMarginIsAlreadyResized == NO) {
self.topMarginIsAlreadyResized = YES; // Once that keyboard has shown when view appears, we should hide it manually
[self.myTextView resignFirstResponder];
}
NSValue *keyboardRectAsObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = CGRectZero;
[keyboardRectAsObject getValue:&keyboardRect];
self.myTextView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardRect.size.height, 0.0f);
}
- (void)handleKeyboardWillHide:(NSNotification *)notification {
self.myTextView.contentInset = UIEdgeInsetsZero;
}
This happens because yor view controller has set the the property automaticallyAdjustsScrollViewInsets to YES, if you set it to NO everything will be fine. See this question and the accepted answer for more info.

Undock Keyboard Programmatically?

I suppose that it is not possible for us to programmatically undock the virtual keyboard. If possible, of course, I would like to know how. I also suppose that it is not possible for us to turn the Split Keyboard switch (General > Keyboard) on programmatically.
Anyway, my situation is the following. I have a tableview control at the top and a textview control right below it with a toolbar control at the bottom. The textview control is editable. So if a user touches it, the virtual keyboard will open, covering the bottom toolbar control. And this keyboard will cover the buttons on the toolbar control. What can I do so that a user can have access to these buttons? I do have a notification ready with UIKeyboardDidShowNotification and UIKeyboardWillHideNotification so that I can tell when a user touches the textview control. Place the toolbar control somewhere else other than at the very bottom? I hope I don't have to do that. Maybe, move the entire view way up while the keyboard is up? I think I can do that.
Thank you for your advice.
I've just decided to move the entire frame upwards when the virtual keyboard is open. It doesn't look bad.
- (void)keyboardWasShown:(NSNotification*)aNotification {
// NSLog(#"It's appeared.");
keyboardup = true;
[self.view setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y-300,self.view.frame.size.width,self.view.frame.size.height)];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
// NSLog(#"It's gone");
keyboardup = false;
[self.view setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+300,self.view.frame.size.width,self.view.frame.size.height)];
}
- (void)keyboardCallingNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}

Resources