Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

swift3 - Custom Keyboard Storyboard with Xcode 8 beta

I'm having some problems designing my custom keyboard from the storyboard in Xcode 8 beta 6.
For some reason when I launch the keyboard on a iOS 10 device this is the result: enter image description here

This is how i design it in the Storyboard, Top View:
enter image description here

Bottom View:enter image description here



So it displays only the height of the bottom view.
I don't have this problem with iOS 9.
Any ideas on what is going wrong?

UPDATE:
this it's how the keyboard gets loaded in iOS 9:
enter image description here

UPDATE 2:
Even creating the view programmatically this way in viewDidLoad() doesn't work:

self.view.backgroundColor = UIColor.orange

let bottomView = UIView()

bottomView.backgroundColor = UIColor.blue

self.view.addSubview(bottomView)

bottomView.translatesAutoresizingMaskIntoConstraints = false

let trailing = bottomView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
let leading = bottomView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
let bottom = bottomView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -50)
let top = bottomView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0)

NSLayoutConstraint.activate([trailing, leading, bottom, top])

I wrote to apple bug report.
Hope to get news from them

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had this same issue. I assume you don't have a height set on the green block because you want it to fill up the remaining space of the keyboard. If you want the keyboard to be a CONSTANT height you can simply set a height constraint on the green block and you're done, but because of screen sizes and orientations you probably don't want one height for everything.

In iOS 9 the default size for a custom keyboard was the same as the system keyboard. So if you run this in iOS 9, the green block fills up the remaining space based on those dimensions. In iOS 10 for some reason there is no default height, and because your green block has no height constraint it thinks the height is zero.

To fix it you need to set a height for your keyboard. This is the code I wrote to handle it (and so far so good). Place this in the keyboardviewcontroller class before the ViewDidLoad and you should be good to go:

//***************************

//create constraint variable before function
var constraint = NSLayoutConstraint()

//function to set height
func setKeyboardHeight () {
    let screenSize = UIScreen.mainScreen().bounds.size
    let screenH = screenSize.height;

    self.view.removeConstraint(constraint)

    //you can set the values below as needed for your keyboard
    if screenH >= 768 {
        //for iPad landscape or portrait
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 300.0)
        self.view.addConstraint(self.constraint)

    } else if screenH >= 414 {
        //for iPhone portrait AND iPhone Plus landscape or portrait
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 220.0)
        self.view.addConstraint(self.constraint)

    } else {
        //for iPhone landscape
        self.constraint = NSLayoutConstraint(item: self.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0.0, constant: 140.0)
        self.view.addConstraint(self.constraint)
    }
}

//sets height when keyboard loads
override func updateViewConstraints() {
    super.updateViewConstraints()
    // Add custom view sizing constraints here
    setKeyboardHeight()
}

//sets or changes height when device rotates
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    setKeyboardHeight()
}

//***************************

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...