by

I have a view with a text field in it, but the text field is at the bottom. When I click the keyboard, it pops up and covers the text field, this is my problem. I was wondering if it is at all possible to push the rest of the view up when a keyboard comes up?

1 Answer

0 votes
by

You can register your view controller to be notified when the keyboard is about to be shown, then you push your view up.

lass ViewController: UIViewController {

var keyboardAdjusted = false
var lastKeyboardOffset: CGFloat = 0.0

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
    if keyboardAdjusted == false {
        lastKeyboardOffset = getKeyboardHeight(notification)
        view.frame.origin.y -= lastKeyboardOffset
        keyboardAdjusted = true
    }
}

func keyboardWillHide(notification: NSNotification) {
    if keyboardAdjusted == true {
        view.frame.origin.y += lastKeyboardOffset
        keyboardAdjusted = false
    }
}

func getKeyboardHeight(notification: NSNotification) -> CGFloat {
    let userInfo = notification.userInfo
    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
    return keyboardSize.CGRectValue().height
}

}

Related questions

0 votes
    How can we make a UITextField move up when the keyboard is present - on starting to edit?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Can anyone help me, Im trying to make a simple app where you touch the screen and 4 images are ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    How to change Swift version from 5 to 4 in Xcode?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    Differentiate between the basic and the advanced on -screen keyboard ? Any 5 points Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Can we use both swift (.swft) and objective c (.h .m) in same xcode project ?...
asked Mar 9, 2021 in Technology by JackTerrance
0 votes
    I have a UITableView of customers where a row/customer is selected. The user can then push another view ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I have a UITableView of customers where a row/customer is selected. The user can then push another view ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    I have a UITableView of customers where a row/customer is selected. The user can then push another view ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    I have a UITableView of customers where a row/customer is selected. The user can then push another view ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Hello I am quite new to pygame and I am trying to make an intro for a game where the user hovers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    1 What is the default view of writer?a Print Viewb. Web Layout Viewc.full screen view 2. Where on the screen ... c. none of these Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Which distribution comes up a lot in Bayesian statistics because it is a good model for one's prior beliefs about ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
0 votes
    There are three coins. One is a two headed coin (having head on both faces), another is a biased coin that ... the two headed coin? Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
...