TextField- KeyBoard Optimization - iOS [duplicate] - ios

This question already has answers here:
How can I make a UITextField move up when the keyboard is present - on starting to edit?
(98 answers)
Closed 9 years ago.
Whenever I click on textfield, keyboard pops up and partially blocked other textfields as seen in the image, how could I manage it? I want whenever last textfield on the right bottom filled, then second row should be up and visible ! By the way, Scroll view on the top of ViewController.
-(void)ViewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,-260,1030,768)];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,1030,768)];
}

Wrap your view in an UIScrollView!

You need to change the view size when the keyboard comes up and change it back when keyboard disappears. Use a scrollview and the keyboard notification or textfield delegate methods for when keyboard appears/disappears.

You need to setup observation of UIKeyboardWillChangeFrameNotification and UIKeyboardDidChangeFrameNotification or UIKeyboardDidShowNotification and UIKeyboardDidHideNotification. When you receive the notifications you will have the trigger to move/resize the content of your view and the size of the keyboard that's being displayed so you know how much to move/resize it.
You may also find it useful to act as the delegate of the text fields and implement textFieldDidBeginEditing:. When this method is called you know that the supplied text field should be visible. You can get the frame of the text field (which you may need to translate if there are multiple superviews before you get to the scroll view) and use it to scrollRectToVisible:animated: (or just set the contentOffset).

Related

iPhone keyboard sizes

Is there a way to get the keyboard size programmatically before the keyboard is presented? In Objective-C
I need to set view.height constraint to be the same as keyboard.height programmatically. And it needs to happen before the keyboard is presented, so the view don't get this ugly constrain animation after the ViewController is presented.
I assume you present the keyboard by calling becomeFirstResponder on some UI component.
If the keyboard appears after your view is presented, you should check where that call is performed. Calling it in viewDidLoad or similarly early should cause the keyboard to be shown as the view animates in.
Your layout should also handle the keyboard changes properly. The keyboard size can change even after it's presented. For example the emoji/quick type keyboards are taller than the default keyboard.
You should perform your constraint changes in a combination of UIKeyboard[Will/Did]ShowNotification, UIKeyboard[Will/Did]HideNotification and UIKeyboardWillChangeFrameNotification. In your case, UIKeyboardWillShowNotification should do the trick.
The userInfo dictionary contains a lot of information about the keyboard. You find the final frame of the keyboard in UIKeyboardFrameEndUserInfoKey. If you animate the changes in your layout, you can use values in UIKeyboardAnimationCurveUserInfoKey and UIKeyboardAnimationDurationUserInfoKey to animate with the same animation as the keyboard.
- (void)viewDidLoad {
[super viewDidLoad];
// Don't forget to remove the observer when appropriate.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[self.textField becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[self.viewHeightConstraint setConstant:keyboardHeight];
// You can also animate the constraint change.
}
Such setup will also work if the keyboard is presented from the get-go.

How To move UITextView above the keyboard in iOS8?

In My app, I have an form with contains UITableView and inside UITableview I have place Mutiple UITextField and UITextView.
Whenever i click on UITextfield or UITextView Keypad comes up.
I am able to move the TextFiled UP when user click on UITextField ones they want to enter the data.But i am now able to move the TextView up when user want to enter the data.
Following is my code:
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
Can anyone help me out to move the UITextView UP above the keypad?
Thanks in Advance.
You can use this library
https://github.com/michaeltyson/TPKeyboardAvoiding
It will handle with frame when show hide keyboard for uitextfiled and uitextView.
And you make sure you do like this:
Adding a "container" view in the case of a ScrollView with constraints set to the superview is also a good practice as follows: ViewController -> UIScrollView (TPKeyboardAvoidingScrollView) -> UIView (container for UI elements) -> UITextView. If you want vertical scrolling set the height constraint to be '>='.
Use IQKeyboardManager to handle this problem.
Features:
1) CODELESS, Zero Line Of Code
2) Works Automatically
3) No More UIScrollView
4) No More Subclasses
5) No More Manual Work
6) No More #imports
And its available for both languages (Swift and Objective C)

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

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...)

How to pin a UIView to the bottom of its superview, but have it move when the keyboard appears?

I have a UIView with a UIView and UITableView in it. The child UIView is for composition, it has textfield and a Send button like any messenger. How can I pin/fix this child UIView to the bottom of the Superview and keep it visibly on top of the UITableView? However the child UIView needs to also rise above the keyboard when its textfield is focused.
You can do this manually. You can sign up for getting keyboard notifications.
You can the following functions
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
There is a very good article about this on Apple Keyboard Management with code snippets.
The answer to your question is different for AutoLayout and for AutoResizingMasks (struts and springs) style layout.
For AutoLayout, the default for Xcode 6, what you would do would be to create a constraint to pin your view to the bottom of it's superview, as normal. Then you'd select that constraint in IB and control-drag it into the header file of your view controller, and create an IBOutlet for the constraint.
You can then write a notification handler for the keyboard will show notification, and in that handler, change the constant (offset) of your constraint property. The final step is to put a call to your superview's layoutIfNeeded method inside a UIView animation block. That causes the layout change from updating the constraint to be animated.
You then do the opposite for a keyboard will hide notification.
If you're doing struts-and-springs style layout, it's similar but simpler. You just create an outlet to the view that you want to move, and then make your keyboard will show notification handler use a UIView animation to animate a change to the view's center property .

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