0

I'm generating a KeyboardView that slides up vertically and then occupies the bottom 1/3 of a view that is used to generate it:

class KeyboardView: UIView {

    private var width: CGFloat!
    private var height: CGFloat!

    init(withinView view: UIView) {
        super.init(frame: view.frame)
        configureKeyboard()
    }

    private func configureKeyboard() {
        width = frame.width
        height = frame.height * 0.33
        center = CGPoint(x: width / 2, y: frame.height - halfHeight)
        let origin = CGPoint(x: center.x - halfWidth, y: (center.y - halfHeight) + height)
        let size = CGSize(width: width, height: height)
        frame = CGRect(origin: origin, size: size)    
        isUserInteractionEnabled = true

    }

}

I then add the keys of this keyboard as UIButton instances after the configureKeyboard method has been called.

However, when running this code on, say, an iPhoneX the keys of this keyboard are not within the so-called safe area of the iPhone screen.

Should I be adjusting the frame.origin value to accommodate the bottom inset? i.e.

frame.origin.y += UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0.0

or is there another way to achieve this programatically?

Thanks for any help, still adjusting to using auto layout.

ajrlewis
  • 2,968
  • 3
  • 33
  • 67
  • 1
    What is the goal here? There is a right way to make a custom keyboard and this is not it. – matt Dec 02 '18 at 17:34
  • @matt To make a custom numerical keyboard as a pop-up view. The implementation works well, but I'm wondering about the autolayout issues ... Thanks. – ajrlewis Dec 02 '18 at 17:58
  • Ok but as I say this is not how to make a custom numerical keyboard. You need an input view. – matt Dec 02 '18 at 18:02
  • @matt I have delegate methods for this `KeyboardView` class that communicate to a `UILabel`. Is this what you mean by an input view? – ajrlewis Dec 02 '18 at 18:05
  • @matt Any views on the auto layout question, please? Thanks – ajrlewis Dec 02 '18 at 18:16
  • " this what you mean by an input view?" No, I mean an input view: https://developer.apple.com/documentation/uikit/uiinputview. If you used this, you'd get a true custom keyboard presented by the system in the correct way. — Would it help you to see an example? I have an example here of a UIPickerView connected to a UILabel, much like what you seem to want to do: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/a23f4ae7a2970be47069ca6d3117d39fa03e7644/bk2ch10p525InputViewTest2/InputViewTest/ViewController.swift – matt Dec 02 '18 at 20:43

0 Answers0