简体   繁体   English

如何在视图出现的同时显示键盘并向上移动 UIView?

[英]How do I display the keyboard and move up the UIView at the same time when the view appears?

I've created a view controller with a UIView containing a text field (Note: the view controller appears as a modal).我创建了一个视图 controller ,其中 UIView 包含一个文本字段(注意:视图 controller 显示为模态)。 When you tap into the text field, a keyboard appears and the UIView moves up the screen so that the text field is not obscured.当您点击文本字段时,会出现一个键盘,并且 UIView 在屏幕上向上移动,这样文本字段就不会被遮挡。 However, my goal is to display the keyboard and the (unobscured) UIView from the very beginning when the modal initially appears , which I'm struggling to achieve.但是,我的目标是从一开始就显示键盘和(未遮挡的)UIView,这是我正在努力实现的模式。

I've tried inserting textField.becomeFirstResponder() into viewDidLoad, but this displays the keyboard without moving the UIView to its desired (ie visible) position.我尝试将 textField.becomeFirstResponder() 插入 viewDidLoad,但这会显示键盘,而不会将 UIView 移动到所需的(即可见)position。 I've also tried inserting this into viewDidAppear, but this displays the UIView first, then stalls for a second, before displaying the keyboard and moving up the UIView in a very awkward transition.我也尝试将其插入 viewDidAppear,但这首先显示 UIView,然后停顿一秒钟,然后显示键盘并在非常尴尬的过渡中向上移动 UIView。

It would be great if anyone could provide some suggestions to solve this issue.如果有人能提供一些建议来解决这个问题,那就太好了。 My code is below.我的代码如下。

@IBOutlet var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .darkGray
    
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    //The line below will display the keyboard over the UIView, thus obscuring it.
    textField.becomeFirstResponder()
}

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
    }
    let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    self.view.frame.origin.y = 0 - keyboardSize!.height
}

Below is a visual reference.下面是视觉参考。

在此处输入图像描述

You can add animation block for your view to look better, also you will need to match the speed for the keyboard animation like this:您可以添加 animation 块以使您的视图看起来更好,您还需要匹配键盘 animation 的速度,如下所示:

        if let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double {
            UIView.animate(withDuration: duration, animations: { [weak self] in
                self?.view.layoutIfNeeded()
            })
        }

This should be added at the end of your keyboardWillShow func.这应该添加到您的keyboardWillShow 函数的末尾。

Also may be it would be better if you move the textField.becomeFirstResponder() to the viewWillAppear.如果您将textField.becomeFirstResponder()移动到 viewWillAppear,也可能会更好。

Make a reference ( IBOutlet ) of your view's bottom constraint, name it as bottomConstraint or whatever you prefer.对视图的底部约束进行引用 ( IBOutlet ),将其命名为bottomConstraint或您喜欢的任何名称。

Then as what you're doing in your keyboardWillShow selector, extract the keyboard height, and assign that height to the constant property of your bottomConstraint .然后就像您在keyboardWillShow选择器中所做的那样,提取键盘高度,并将该高度分配给您的bottomConstraintconstant属性。 Add animations if you want.如果需要,请添加动画。

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet var textField: UITextField!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .darkGray
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        //The line below will display the keyboard over the UIView, thus obscuring it.
        textField.becomeFirstResponder()
    }

    @objc func keyboardWillShow(_ notification: Notification) {
        let info = notification.userInfo!
        let kbHeight = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
        
        bottomConstraint.constant = -kbHeight
        
        let duration: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        
        UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
    }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM