如何使键盘与视图大小相同

How to make the keyboard same size as the view

在下面的代码中,UIKeyboard 比视图大。

问:如何使键盘与视图大小一致?

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController, UITextFieldDelegate {
    override func loadView() {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.backgroundColor = .white

        let label = UITextField()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black

        label.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)

        view.addSubview(label)

        self.view = view
    }

    @objc func myTargetFunction() {
        print("It works!")
    }

}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

到目前为止,我发现的唯一 "solution" 是将 UIViewController 的 preferredContentSize 设置为当前 Xcode 游乐场环境的 UIScreen.main 的大小:

let vc = MyViewController()
vc.preferredContentSize = UIScreen.main.bounds.size
PlaygroundPage.current.liveView = vc