键盘正在swift中隐藏UITextField

时间:2015-01-15 15:09:21

标签: swift

我有一个按代码添加的UITextField,当键盘打开时,它会隐藏文本字段。 我搜索了很多并得到了相同的结果,她是我的代码,如苹果文档建议,但它仍然没有用!

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;
    }

}

2 个答案:

答案 0 :(得分:1)

(假设数学是正确的)你应该使用的是UIKeyboardFrameEndUserInfoKey而不是UIKeyboardFrameBeginUserInfoKey。
请查看Keyboard Notification User Info Keys

的文档

答案 1 :(得分:1)

我找到了缺失的部分, 滚动视图应更改以下内容:

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;