shajem
shajem

Reputation: 2121

UITextfield to be displayed on top of Keyboard - Beginner

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?

Upvotes: 5

Views: 12289

Answers (2)

Ravi Kumar
Ravi Kumar

Reputation: 1366

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];
}

Upvotes: 0

Andres Canella
Andres Canella

Reputation: 3716

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

Upvotes: 2

Related Questions