dimanche 28 juin 2015

iOS Keyboard in Swift is too wide and tall when loading from XIB file

I'm building a keyboard using Swift (XCode 6 with iOS 8.3 SDK) and when I load the xib, the keyboard is about 1000 pixels too wide and tall. I've, in the freeform xib file, put all my keys in a UIView and set the UIViews constraints to superview top, bottom, left and right.

http://ift.tt/1LOUAOA

As you can see, the result is annoying. Here's my code:

import UIKit

class KeyboardViewController: UIInputViewController {

var BlurBoardView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    loadInterface()
}

func loadInterface() {
    //load the nib file
    var blurboardNib = UINib(nibName: "BlurBoardView", bundle: nil)
    // initiate the view
    BlurBoardView = blurboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView
    // add the interface to the main view
    view.addSubview(BlurBoardView)
    // copy the background color
    view.backgroundColor = BlurBoardView.backgroundColor

    let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
    blur.frame = view.frame
    view.addSubview(blur)
}

@IBAction func didTapButton(sender: AnyObject?) {

    let button = sender as! UIButton
    let title = button.titleForState(.Normal)
    var proxy = textDocumentProxy as! UITextDocumentProxy

    switch title as String!{
        case "<" :
            proxy.deleteBackward()
        case "RETURN" :
            proxy.insertText("\n")
        case " " :
            proxy.insertText(" ")
        case "CHG" :
            self.advanceToNextInputMode()
        default :
            proxy.insertText(title!)
    }
}

/*
override func textWillChange(textInput: UITextInput) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(textInput: UITextInput) {
    // The app has just changed the document's contents, the document context has been updated.

    var textColor: UIColor
    var proxy = self.textDocumentProxy as! UITextDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
        textColor = UIColor.whiteColor()
    } else {
        textColor = UIColor.blackColor()
    }
    //self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
*/
}

The keyboard, in it's precompiled xib looks like this:

http://ift.tt/1KiPACZ

You can also see the parent (of the keys) views constraints.

I'm mainly an Objective-C developer, so the code makes sense, but the issue might just be a huge SBE because of Swift, so sorry if it's ridiculously simple ;)

Thanks.

Aucun commentaire:

Enregistrer un commentaire