0

我正在尝试使用 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

它应该看起来像这样

正确按钮

4

1 回答 1

0

问题写在日志中:

Unable to simultaneously satisfy constraints.
    ... 
(
    "<NSLayoutConstraint:0x1700913f0 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x13f5091e0(216)]>",
    "<NSLayoutConstraint:0x170091d00 V:[UIInputView:0x13f5091e0(100)]>"
)

您可能正在使用情节提要中的自动布局(或代码中的任何其他位置)设置高度。只需删除它(或更改为较低的优先级),它应该设置为 100px。

于 2014-12-17T10:18:32.100 回答