Tyler Patrick
Tyler Patrick

Reputation: 57

Programmatically raise UI text fields when keyboard opens

Here is my current code, and it works, however on smaller devices, the fields are moved above the users display. How can I edit this to make sure the fields do not become hidden from the user? I stole some of this code from a tut by gregbarbosa, so an explanation with an answer would help greatly. Thank you.

import UIKit

class LoginViewController: UIViewController {

//MARK: Storyboard
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var teamID: UITextField!
@IBOutlet weak var scoutID: UITextField!
@IBOutlet weak var pinNum: UITextField!
@IBOutlet weak var signin: UIButton!
@IBOutlet weak var diamondPic: UIImageView!
@IBOutlet weak var nodextext: UILabel!
@IBOutlet weak var loginB: UIButton!
@IBOutlet weak var moverL: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")

    // prevents the scroll view from swallowing up the touch event of child buttons
    tapGesture.cancelsTouchesInView = false

    scrollView.addGestureRecognizer(tapGesture)

}

@IBAction func loginTapped(sender: UIButton) {
    var userDefaults = NSUserDefaults.standardUserDefaults()
    var secretKey = userDefaults.stringForKey("secretkey")
    var team:String = teamID.text
    var scout:String = scoutID.text
    var password:String = pinNum.text


    var loginQuery = PFQuery(className:"User")
    loginQuery.whereKey("teamID", equalTo:team)
    loginQuery.whereKey("scoutID", equalTo:scout)
    loginQuery.whereKey("password", equalTo:password)
    loginQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil && objects.count != 0 {
            self.performSegueWithIdentifier("SecondViewController", sender: self)
        } else if error == nil && objects.count == 0{
            var alert : UIAlertView = UIAlertView(title: "Login Error", message: "Your TeamID, ScoutID, or Password is inncorrect.",       delegate: nil, cancelButtonTitle: "Ok")

            alert.show()

        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }
}
@IBOutlet weak var loginTapped: UIButton!


override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)
    self.registerForKeyboardNotifications()

}

override func viewWillDisappear(animated: Bool) {
    self.deregisterFromKeyboardNotifications()
    super.viewWillDisappear(true)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

func registerForKeyboardNotifications() -> Void {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)

}

func deregisterFromKeyboardNotifications() -> Void {
    println("Deregistering!")
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidHideNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillHideNotification, object: nil)

}

func keyboardWasShown(notification: NSNotification) {
    var info: Dictionary = notification.userInfo!
    var keyboardSize: CGSize = (info[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size)!
    var buttonOrigin: CGPoint = self.loginB.frame.origin;
    var buttonHeight: CGFloat = self.loginB.frame.size.height;
    var visibleRect: CGRect = self.view.frame
    visibleRect.size.height -= keyboardSize.height

    if (!CGRectContainsPoint(visibleRect, buttonOrigin)) {
        var scrollPoint: CGPoint = CGPointMake(0.0, buttonOrigin.y - visibleRect.size.height + buttonHeight + 4)
        self.scrollView.setContentOffset(scrollPoint, animated: true)

    }
    nodextext.hidden = true
    diamondPic.hidden = true
}

func hideKeyboard() {
    teamID.resignFirstResponder()   //FirstResponder's must be resigned for hiding keyboard.
    scoutID.resignFirstResponder()
    pinNum.resignFirstResponder()
    self.scrollView.setContentOffset(CGPointZero, animated: true)
    diamondPic.hidden = false
    nodextext.hidden = false
}

}

Thank you in advanced.

Upvotes: 0

Views: 89

Answers (2)

HamzaGhazouani
HamzaGhazouani

Reputation: 6604

I think the best way to hide keyboard is to add resignFirstResponder (or call your hideKeyboard) in scrollView delegate method :

  func scrollViewWillBeginDragging(scrollView: UIScrollView)

Hope this helps

Upvotes: 1

Ian
Ian

Reputation: 12758

There are a couple ways to do this. First, you could change the content offset of the scroll view manually when a UI Element becomes first responder. You can check for this using UITextFieldDelegate and the textFieldDidBeginEditing: method. This often proves to be difficult though. Another thing to do is to put all of your UI Elements in a table view and call scrollToRowAtIndexPath:atScrollPosition:animated: when a UIElement such as a text field becomes first responder.

Upvotes: 1

Related Questions