0

I have a UIViewController with two containers embedded and one textfield. So far when user taps the textfield the whole screen moves up so the keyboard can fit without covering the lower part of the containers. This is how it looks in my story board:

enter image description here

My code looks as follows:

override func viewDidLoad() {
    super.viewDidLoad()
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
     //    view.addGestureRecognizer(tap)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillChangeFrameNotification, object: nil)

}

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {

        if(isKeyboardShown == false){
            realKeyboardSize = CGRect(x: keyboardSize.origin.x, y: keyboardSize.origin.y, width: keyboardSize.width, height: keyboardSize.height)
            isKeyboardShown = true
            self.view.frame.origin.y -= realKeyboardSize!.height
        }else{
            isKeyboardShown = false
            self.view.frame.origin.y += realKeyboardSize!.height

        }
    }
}

Is it possible to move the lower container up instead of the whole screen?

I imagine it working like this:

topContainer stays untouched, lowerContainer moves up so that half of it is hidden behind topContainer and the keyboard is visible. When user hides the keyboard everything comes back to normal.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
user3766930
  • 5,629
  • 10
  • 51
  • 104
  • is you are using autolayout check http://stackoverflow.com/questions/31356293/uitableview-and-uiview-with-keyboardwillshow/31356527#31356527 – Bhavin Bhadani Apr 19 '16 at 09:06
  • thanks, I did what you suggested and added those lines in viewDidLoad: ` NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)` But now when I tap the textfield it completely disappear :) I tried adding only `keyboardWillShow` and then the container moves up, but I don't see the keyboard itself... – user3766930 Apr 19 '16 at 09:18
  • hm for now the keyboard appears, but the screen does not move up. However when I hide the keyboard then the screen goes down a little and the textfield disappears. Do you know what might be the problem here? – user3766930 Apr 19 '16 at 09:35
  • so far I added constraints to the `lowerContainer` only, then I did as you suggested in your answer - do you think it is enough? – user3766930 Apr 19 '16 at 09:41

2 Answers2

1

If you are using autolayout, you can achieve this by changing bottom constraint runtime

Here is your hierarchy and related constraints..

enter image description here

For how it works and how to setup things check this link

and Here is the demo, if you don't understand

Community
  • 1
  • 1
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
1

take outlet of container's bottom constraint and increase it's constant to keyboard size or take outlet of top constraint and decrease it's constant to keyboard size . hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75