user1386213
user1386213

Reputation: 1021

keyboard is hiding UITextField in swift

i have a UITextField added by code, and when the keyboard is opened, it hides the textfield. i searched a lot and got the same results and her is my code as apple documentation suggest, but still it is not working!

import Foundation
import UIKit

class testkey: UIViewController, UITextFieldDelegate {

    var txt1: UITextField! = nil
    var scrollView: UIScrollView! = nil

    var activeTextField: UITextField!

    override func viewDidLoad() {

        txt1 = UITextField(frame: CGRect(x: 0, y:250, width: 223, height: 30))
        txt1.text = "text"
        txt1.borderStyle = UITextBorderStyle.RoundedRect
        txt1.delegate = self

        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 225.00, height: 290));
        scrollView.addSubview(txt1)

        self.view.addSubview(scrollView)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    // Call this method somewhere in your view controller setup code.
    func registerForKeyboardNotifications() {
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self,
            selector: "keyboardWillBeShown:",
            name: UIKeyboardWillShowNotification,
            object: nil)
        notificationCenter.addObserver(self,
            selector: "keyboardWillBeHidden:",
            name: UIKeyboardWillHideNotification,
            object: nil)
    }

    // Called when the UIKeyboardDidShowNotification is sent.
    func keyboardWillBeShown(sender: NSNotification) {
        let info: NSDictionary = sender.userInfo!
        let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue
        let keyboardSize: CGSize = value.CGRectValue().size
        let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets

        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your app might not need or want this behavior.
        var aRect: CGRect = self.view.frame
        aRect.size.height -= keyboardSize.height
        let activeTextFieldRect: CGRect? = activeTextField?.frame
        let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
        if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
            scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
        }
    }


    // Called when the UIKeyboardWillHideNotification is sent
    func keyboardWillBeHidden(sender: NSNotification) {
        let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
    }

    func textFieldDidBeginEditing(textField: UITextField!) {
        activeTextField = textField
        scrollView.scrollEnabled = true
    }

    func textFieldDidEndEditing(textField: UITextField!) {
        activeTextField = nil
        scrollView.scrollEnabled = false
    }

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

    override func viewDidDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
    {
        textField.resignFirstResponder()
        return true;
    }

}

Upvotes: 0

Views: 565

Answers (2)

user1386213
user1386213

Reputation: 1021

i found the missing part, the following should be changed for the scroll view:

scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height));
        scrollView.contentSize = self.view.frame.size;

Upvotes: 1

nacho4d
nacho4d

Reputation: 45128

(Assuming the maths are correct) what you should use is UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey.
Please check the Documentation of Keyboard Notification User Info Keys

Upvotes: 1

Related Questions