Calculate button height of keyboard buttons

139 views Asked by At

I'm working on a universal keyboard extension. Currently I want to calculate the height of a single button. The keyboard has 4 rows as the normal iOS keyboard. Because the space between the buttons (y distance) is 5 I calculated: (self.view.frame.height-(5*5))/4 5x5 because the distance to the top and the bottom is included. Now, if I run the app, one button isn't around 1/4 of the screen. Instead the button's height is around 2/3 of the keyboard view. I really can't find my mistake. I hope you can help me with this calculation.

My current code:

import UIKit

class KeyboardViewController: UIInputViewController, UIScrollViewDelegate {


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

override func viewDidLayoutSubviews() {

    print((self.view.frame.height-25)/4)
    addButtons()
}

func addButtons() {

    let buttonHeigth = (self.view.frame.height-25)/4
    let button = ResizableButton()
    button.frame = CGRect(x: CGFloat(5), y: CGFloat(10), width: CGFloat(button.frame.size.width), height: CGFloat(buttonHeigth))
    button.addTarget(self, action: #selector(keyPressed(sender:)), for: .touchUpInside)
    button.layer.cornerRadius = 10
    button.layer.masksToBounds = false
    self.scrollView.addSubview(button)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

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
    let proxy = self.textDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
        textColor = UIColor.white
    } else {
        textColor = UIColor.black
    }
    self.nextKeyboardButton.setTitleColor(textColor, for: [])
}
}
0

There are 0 answers