键盘上方的视图未显示

时间:2019-08-07 20:32:26

标签: swift iphone xcode

显示键盘时,我想在键盘上方有一个带textField的视图和一个发送按钮。但这不起作用。

我已经在我的代码中实现了这一点:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBarController?.tabBar.isHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        tabBarController?.tabBar.isHidden = false

    }

    // MARK: - Keyboard stuff
    @objc func keyboardWillShow(_ notification: NSNotification) {
        let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue   
        print("hier ist \(keyboardFrame!)")
        UIView.animate(withDuration: 0.1) {
            self.bottomConstraint.constant = keyboardFrame!.height     
            self.view.layoutIfNeeded()
        }
    }

    @objc func keyboardWillHide(_ notification: NSNotification) {
        UIView.animate(withDuration: 0.1) {
            self.bottomConstraint.constant = 0
            self.view.layoutIfNeeded()
        }
    }

1 个答案:

答案 0 :(得分:0)

签出textField inputAccessoryView。有一个教程here说明了您要做什么。