迅速操场UITextField生成太大的键盘 [英] swift playground UITextField spawns keyboard that is too big
问题描述
在Mac OS上的操场上快速移动.当用户单击UItextfield时,会生成一个键盘,但是与视图相比,它很大,并且只有前几个键可用.
Swift in playground on Mac OS. When the user clicks in a UItextfield, a keyboard spawns but it is very large compared to the view and only the first few keys are available.
最小示例:
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()
屏幕截图
推荐答案
我遇到同样的问题.看来Playground的硬编码屏幕尺寸为768x1024(在Playground中运行UIScreen.main.bounds
),并根据该尺寸显示键盘,而与实时取景的实际尺寸无关.
I face the same issue. It seems as if Playground has a hard-coded screen size of 768x1024 (run UIScreen.main.bounds
in the Playground) and shows the keyboard according to this size, independently of the live view's actual size.
我想出的最好的解决方法是增加视图控制器的大小,使其与键盘匹配:
The best workaround I came up with is to increase the size of the view controller so that it matches the keyboard:
let vc = TesterViewController()
vc.preferredContentSize = vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc
当然,这会使视图变得比您想要的要大,因此,仅在确实需要访问屏幕键盘进行测试时,才使用此变通方法.
Of course this makes the view larger than you might want it to be, so I only use this workaround when I really have to access the on-screen keyboard for testing.
这篇关于迅速操场UITextField生成太大的键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!