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

时间:2017-11-02 12:20:23

标签: swift-playground

在以下代码中,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()

1 个答案:

答案 0 :(得分:0)

到目前为止,我发现的唯一“解决方案”是将UIViewController的preferredContentSize设置为当前Xcode Playground环境的UIScreen.main的大小:

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