UITextfield will appear on top of the keyboard - newbie

I have one textboxes. When I click on it, a keyboard appears. but since it uitextfieldโ€™s somewhere below, it closes when the keyboard pops up. (The keyboard is displayed above the text box, so the user cannot see it).

I need a way to move textfieldup so the user can see it. The iPhone application facebookhas a workaround, it reduces UIComponentsin the view to display textfieldon top, so it does not close from the keyboard.

Can someone point me to a tutorial or sample code to get started?

+5
source share
4 answers

working demonstration

iOS9 , .

var defaultFrame: CGRect!
  1. init()

defaultFrame = self.frame
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

func moveViewWithKeyboard(height: CGFloat) {
    self.frame = CGRectOffset(defaultFrame, 0, height)
}

func keyBoardWillShow(notification: NSNotification) {
    let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    moveViewWithKeyboard(-frame.height)
}
func keyBoardWillHide(notification: NSNotification) {
    moveViewWithKeyboard(0)
}
+2

- UIScrollView , . /

UITextField ,

+1

This may help you ... bottomLayoutConstrain is the lower limit from the text box at the bottom of the window where you want to go from. Quick 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)
    }

Target 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];
}
0
source

All Articles