I'm trying to create an iOS8 custom keyboard with swifts. So far my app is running well, I just have problems with the size of my UIImage buttons. I'm setting the keyboard size with the following method to 100 pixel:
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))
And then I add the key buttons in the following way:
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])
My key1.jpg is exact 100 pixel high. It does fit exactly into the size of my keyboard area (Height = 100), but both is much to big. So it seems that each pixel taxes 2 pixel of the real retina resolution or more. Also the width is much to much. I tested with a 400 pixel wide key and just around the half fits on the screen. I also tried with an .png file, same result. There must be some general settings wrong, but I really have no idea what.
Checking the error output I got the following message:
Unable to simultaneously satisfy constraints.
...
(
"<NSLayoutConstraint:0x1700913f0 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x13f5091e0(216)]>",
"<NSLayoutConstraint:0x170091d00 V:[UIInputView:0x13f5091e0(100)]>"
)
I did set the high to 100, but where does the high of 216 come from?
Here how it looks like
(source: modellautos24.de)
and it should look like that