5

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?

Parth Bhatt
  • 19,085
  • 27
  • 131
  • 216
shajem
  • 2,031
  • 5
  • 20
  • 29
  • Yes, i am looking at it now :) I'll update you if anything pops out. – shajem Feb 26 '12 at 16:06
  • Not yet, still trying. The text field height got increased after adding that code. – shajem Feb 26 '12 at 17:12
  • possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – jscs Feb 26 '12 at 19:01
  • @shajem: Whats the problem you are facing still? – Parth Bhatt Mar 02 '12 at 07:15

4 Answers4

5

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.

Community
  • 1
  • 1
Parth Bhatt
  • 19,085
  • 27
  • 131
  • 216
  • It says that `UIKeyboardBoundsUserInfoKey` is deprecated. Do you know any alternative for this ? – shajem Feb 26 '12 at 16:13
  • @shajem: You can refer this link for the solution to deprecation of `UIKeyboardsUserInfoKey` http://stackoverflow.com/questions/2807339/uikeyboardboundsuserinfokey-is-deprecated-what-to-use-instead – Parth Bhatt Feb 26 '12 at 16:32
  • 4
    While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Feb 27 '12 at 00:22
  • @BilltheLizard: If I copy their answers and put it here and then provide the link as reference then in that case, I think that the guy who deserves the credit for the right answer won't get his due credit. Please let me know if I am wrong. Nothing personal :) Thanks – Parth Bhatt Feb 27 '12 at 04:58
  • 3
    Please summarize the answer here and include the link for reference. If the links rot, then your current answer will become worthless. This is why we ask people to *completely* answer the question here. – Bill the Lizard Feb 27 '12 at 11:40
  • @BilltheLizard: Ok thanks :) I will keep this in mind from now onwards – Parth Bhatt Feb 27 '12 at 12:11
2

working demo

Working on iOS9 on spacial cases such as predictive text open and close and different size keyboards.

  1. Global variable to store your default view frame

var defaultFrame: CGRect!
  1. 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)
  1. Simple function to relocate your view

func moveViewWithKeyboard(height: CGFloat) {
    self.frame = CGRectOffset(defaultFrame, 0, height)
}
  1. 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)
}
Andres Canella
  • 3,592
  • 1
  • 30
  • 45
  • doesnt work on ios 11. input field disappears as soon as i press a key on the keyboard – dy_ Nov 26 '17 at 19:40
1

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

Community
  • 1
  • 1
Amit Shah
  • 4,127
  • 2
  • 19
  • 26
0

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];
}
Ravi Kumar
  • 1,249
  • 12
  • 21