swift playground UITextField产生的键盘太大了

Dav*_*ard 9 keyboard uitextfield swift-playground

Swift在Mac OS的操场上.当用户点击UItextfield时,键盘会产生,但与视图相比它非常大,只有前几个键可用.

最小的例子:

import UIKit
import PlaygroundSupport

class TesterViewController : UIViewController {
var testTextField : UITextField!
override func loadView() {
    let view = UIView()
    view.backgroundColor = .white

    testTextField = UITextField()
    testTextField.borderStyle = .roundedRect
    testTextField.text = ""
    view.addSubview(testTextField)
    testTextField.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        testTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
        testTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
        ])

    self.view = view
    }
}
PlaygroundPage.current.liveView = TesterViewController()
Run Code Online (Sandbox Code Playgroud)

截图 在此输入图像描述

dr_*_*rto 9

我面临同样的问题.似乎Playground的硬编码屏幕大小为768x1024(UIScreen.main.bounds在Playground中运行),并根据此大小显示键盘,与实时视图的实际大小无关.

我想出的最佳解决方法是增加视图控制器的大小,使其与键盘匹配:

let vc = TesterViewController()
vc.preferredContentSize = vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc
Run Code Online (Sandbox Code Playgroud)

当然这会使视图比你想象的要大,所以当我真的必须访问屏幕键盘进行测试时,我才使用这种解决方法.