如何在键盘存在时使UITextField向上移动而不使用滚动视图而不是以恒定值移动?

时间:2017-02-19 03:51:55

标签: ios iphone swift keyboard textfield

我的需求:

  1. 根据键盘覆盖的textField以及覆盖的内容来移动UIView。
  2. 不想使用scrollView重建它。
  3. 实现1.我必须知道我选择的textField的坐标Y.很多 使用NSNotificationCenter构建侦听器的答案。有什么我不明白,为什么设置为对象?是不是触发下面功能的对象?我试图获得对象的位置但失败了。因为我将nil设置为object。尝试将textField设置为对象仍无法正常工作。

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: UITextField())
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: UITextField())
    

    如何获取所选textField的位置?如果不宣布这样的事情:

    @IBOutlet var textField: UITextField!
    

    通知中是否有隐藏信息?也许在userInfo?因为键盘的高度就在其中。

    let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    

    这是我的代码:

    import UIKit
    
    class VC_Base: UIViewController, UITextFieldDelegate{
    
        var keyBoardShow:Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VC_Base.keyboardWillShow), name: UIKeyboardWillShowNotification, object: self)
    
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VC_Base.keyboardWillHide), name: UIKeyboardWillHideNotification, object: self)
            // Do any additional setup after loading the view.
        }
        func keyboardWillShow(notification: NSNotification) {
            guard keyBoardShow else{
                adjustingHightLight(true, notification: notification)
                keyBoardShow = true
                return
            }
            keyBoardShow = true
        }
    
        func keyboardWillHide(notification: NSNotification) {
            adjustingHightLight(false, notification: notification)
            keyBoardShow = false
        }
    
        func adjustingHightLight(show: Bool, notification: NSNotification) {
            var userInfo = notification.userInfo!
            if let object = notification.object as! UITextField!{
                print("there is object")
                let textFieldCoverByKeyBoard = self.view.frame.maxY - object.frame.maxY
                let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
                let differenceOfTextFieldAndKeyBoard = textFieldCoverByKeyBoard - keyboardFrame.height
                let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
                let changeInHeight = differenceOfTextFieldAndKeyBoard * (show ? 1 : -1)
                if differenceOfTextFieldAndKeyBoard < 5{
                    UIView.animateWithDuration(animationDuration, delay: 0, usingSpringWithDamping: 0, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
                        self.view.frame.offsetInPlace(dx: 0, dy: changeInHeight)}, completion: nil)
                }
            }
        }
    
        func textFieldShouldReturn(textField: UITextField) -> Bool {
            self.view.endEditing(true)
            return true
        }
    
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.view.endEditing(true)
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    
        }
    }
    
    发短信是它应该实现以下几个方面的基本功能:

    1.键盘是否覆盖textField?

    如果为true,它覆盖多少并向上移动UIView值,键盘将无法覆盖它。   如果是假,不做任何事情并显示键盘。

    2.如果我已选择一个textField,然后选择另一个。它不会再次上升(这将在某些答案中发生)。

    但似乎除了使用scrollView(我不想重建......)之外没有太多的答案实现它。

    请帮助!! THX。

0 个答案:

没有答案