I have textboxes
. When i click on it, the Keyboard pops out. but, since the uitextfield
is located some where below, it gets covered when the keyboard pops out. (The keyboard gets displayed above the text field, so the user is unable to see it).
I need a way to move the textfield
up so that the user can see it. The iPhone facebook
app has a workaround, they shrink the UIComponents
on the view to display the textfield
on top, so it doesn't cover from the keyboard.
Can someone point me to a tutorial or sample code to start with?
OPTION-1: You can refer to this code which has been accepted as answer in this following link:
How to make a UITextField move up when keyboard is present?
This will surely help you and give you your required solution.
OPTION-2: Also here is an ready made TUTORIAL:
http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/
I think you would be surely helped by this one
OPTION-3: You can refer to this:
http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/
OPTION-4: Apple Documentation:
https://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
Hope this helps you.
Working on iOS9 on spacial cases such as predictive text open and close and different size keyboards.
- Global variable to store your default view frame
var defaultFrame: CGRect!
- In init() save your initial frame & subscribe to keyboard notifications
defaultFrame = self.frame
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
- Simple function to relocate your view
func moveViewWithKeyboard(height: CGFloat) {
self.frame = CGRectOffset(defaultFrame, 0, height)
}
- Move based on internal keyobard offset
func keyBoardWillShow(notification: NSNotification) {
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
moveViewWithKeyboard(-frame.height)
}
func keyBoardWillHide(notification: NSNotification) {
moveViewWithKeyboard(0)
}
Best way is to use a UIScrollView
and move it up when you need to started editing text.
This question/answer will give you a rough guide on getting it set-up
How to make a UITextField move up when keyboard is present
It may help you... bottomLayoutConstrain is bottom constraint from
textfield to bottom of view from where you want to up. Swift Code:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShowNotification(notification:NSNotification){
let keyboardInfo:NSDictionary = notification.userInfo!
let keyboardFrameBegin:AnyObject = keyboardInfo.valueForKey(UIKeyboardFrameEndUserInfoKey)!
keyboardFrameBeginRect = keyboardFrameBegin.CGRectValue
self.view.layoutIfNeeded()
self.bottomLayoutConstrain.constant = self.keyboardFrameBeginRect!.height
self.view.layoutIfNeeded()
}
func keyboardWillHideNotification(notification:NSNotification){
self.view.layoutIfNeeded()
bottomLayoutConstrain?.constant = 0
self.view.layoutIfNeeded()
}
deinit {
// perform the deinitialization
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
// NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
Objective c Code
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void) keyboardWillShowNotification:(NSNotification *) notification{
NSDictionary *keyboardInfo = notification.userInfo;
keyboardFrameBeginRect = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[self.view layoutIfNeeded];
// [UIView animateWithDuration:1.0 animations:^{
topLayoutConstraints.constant = -keyboardFrameBeginRect.size.height;
bottomLayoutConstrain.constant = keyboardFrameBeginRect.size.height;
// }];
[self.view layoutIfNeeded];
}
-(void) keyboardWillHideNotification:(NSNotification *) notification{
[self.view layoutIfNeeded];
topLayoutConstraints.constant = 0;
bottomLayoutConstrain.constant = 0;
[self.view layoutIfNeeded];
}