我正在尝试使用 swifts 创建一个 iOS8 自定义键盘。到目前为止,我的应用程序运行良好,只是 UIImage 按钮的大小存在问题。我使用以下方法将键盘大小设置为 100 像素:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy:.Equal, toItem:self.view,
attribute:.Height, multiplier:0.0, constant: 100))
然后我按以下方式添加关键按钮:
let image = UIImage(named: "key1.jpg")
nextKeyboardButton = UIButton.buttonWithType(.System) as UIButton
nextKeyboardButton.setBackgroundImage(image, forState: UIControlState.Normal)
// initialize the button
nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
var nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
var nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
我的 key1.jpg 正好是 100 像素高。它确实适合我的键盘区域的大小(高度 = 100),但两者都很大。因此,似乎每个像素需要 2 个像素的真实视网膜分辨率或更多。宽度也很大。我用一个 400 像素宽的键进行了测试,大约一半适合屏幕。我也尝试使用 .png 文件,结果相同。肯定有一些常规设置错误,但我真的不知道是什么。
检查错误输出,我收到以下消息:
Unable to simultaneously satisfy constraints.
...
(
"<NSLayoutConstraint:0x1700913f0 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x13f5091e0(216)]>",
"<NSLayoutConstraint:0x170091d00 V:[UIInputView:0x13f5091e0(100)]>"
)
我确实将高点设置为 100,但是 216 的高点从何而来?
这里的样子
(来源:modellautos24.de)
它应该看起来像这样