-1

I have made a custom keyboard using a .xib and .swift file. I set it for a textfield by doing the following:

let customNumberPad = CustomNumberPad()
length.inputView = customNumberPad.inputView

However, the size of the keyboard area is large like it is using the old keyboard.

image of issue

I have tried setting the height in the .xib to 200 via constraint.

I have tried:

length.inputView!.autoresizingMask = []
let heightAnch = length.inputView!.heightAnchor.constraint(equalToConstant: 200)
heightAnch.isActive = true
length.reloadInputViews()

Full CustomNumberPad code:

import UIKit
import AudioToolbox

class CustomNumberPadSmall: UIInputViewController {

  @IBOutlet var numberPad: UIView!

  @IBAction func insertText(_ sender: UIButton) {
    if let text = sender.currentTitle {
        AudioServicesPlaySystemSound (1104)
        self.textDocumentProxy.insertText(text)
    }
  }

  @IBAction func backSpace(_ sender: UIButton) {
    self.textDocumentProxy.deleteBackward()
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    overrideUserInterfaceStyle = USERINFO.darkModeValue
    
    Bundle.main.loadNibNamed("CustomNumberPadSmall", owner: self)
    numberPad.translatesAutoresizingMaskIntoConstraints = false
    
    let inputView = self.inputView!
    inputView.translatesAutoresizingMaskIntoConstraints = false
    inputView.addSubview(numberPad)
    
    NSLayoutConstraint.activate([
        numberPad.topAnchor.constraint(equalTo: inputView.topAnchor),
        numberPad.bottomAnchor.constraint(equalTo: inputView.bottomAnchor),
        numberPad.leadingAnchor.constraint(equalTo: inputView.leadingAnchor),
        numberPad.trailingAnchor.constraint(equalTo: inputView.trailingAnchor),
        numberPad.heightAnchor.constraint(equalToConstant: 200)
    ])
  }
}
WillM00
  • 1
  • 1
  • Please search before asking. "UIInputViewController height" gets _lots_ of answers. – matt Jul 07 '22 at 23:14

1 Answers1

0

If you only need a number pad it's better to use keyboardType property of UITextField.

yourTextField.keyboardType = .numberPad

In another case you need a custom keyboard itself, override intrinsicContentSize property of UIInputView

class CustomNumberPad: UIInputView {

    // your calculated input view height
    var intrinsicHeight: CGFloat = 200 {
        didSet {
            self.invalidateIntrinsicContentSize()
        }
    }

    init() {
        super.init(frame: CGRect(), inputViewStyle: .keyboard)
        self.translatesAutoresizingMaskIntoConstraints = false
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.translatesAutoresizingMaskIntoConstraints = false
    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: UIView.noIntrinsicMetric, height: self.intrinsicHeight)
    }
}

How to use :

let customNumberPad = CustomNumberPad()
yourTextField.inputView = customNumberPad
Sreekuttan
  • 1,579
  • 13
  • 19