作者:手机用户2602897931 | 来源:互联网 | 2023-01-12 09:24
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()
截图
1> dr_barto..:
我面临同样的问题.似乎Playground的硬编码屏幕大小为768x1024(UIScreen.main.bounds
在Playground中运行),并根据此大小显示键盘,与实时视图的实际大小无关.
我想出的最佳解决方法是增加视图控制器的大小,使其与键盘匹配:
let vc = TesterViewController()
vc.preferredCOntentSize= vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc
当然这会使视图比你想象的要大,所以当我真的必须访问屏幕键盘进行测试时,我才使用这种解决方法.