首页 > 解决方案 > 自定义iOS键盘:如何根据不同的iPad调整键盘框架

问题描述

这是问题:

所以我在 iPad 上构建了一个自定义的 iOS 键盘,当然,我希望它可以在多个 iPad 上工作。我已经对其进行了设置,以便 UIView 在 XIB 中制作并在我的 InputViewController 中实例化。现在,它设置为我的 XIB 中指定的框架。我希望键盘位于屏幕的下三分之一处,覆盖屏幕的宽度,因此不会溢出。

我尝试在 InputViewController、awakeFromNib()、viewDidLayoutSubviews() 和 viewWillAppear() 中调整 viewDidLoad() 中的框架,但是当我尝试这些时,只出现黑屏。

这是我的代码(视图是bibleKeyboardView

class KeyboardViewController: UIInputViewController {

    // A property to hold reference to a BibleKeyboardView object
    var bibleKeyboardView: BibleKeyboardView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // An instance of BibleKeyboardView is added to the controller's root inputView
        let nib = UINib(nibName: "BibleKeyboardView", bundle: nil)
        let objects = nib.instantiate(withOwner: nil, options: nil)
        bibleKeyboardView = (objects.first as! BibleKeyboardView)
        guard let inputView = inputView else { return }
        inputView.addSubview(bibleKeyboardView)

        // controls visibility of the globe key
        bibleKeyboardView.setNextKeyboardVisible(needsInputModeSwitchKey)

        // adds automatic handle switching to glove key
        bibleKeyboardView.nextKeyboardBtn.addTarget(self,
                                                    action: 
                                                    #selector(handleInputModeList(from:with:)),
                                                       for: .allTouchEvents)

        bibleKeyboardView.delegate = self

        let screenBounds = UIScreen.main.bounds
        let screenWidth = screenBounds.width
        let screenHeight = screenBounds.height

        // This is the part that was causing my blank screen
        bibleKeyboardView.frame = CGRect(x: 0, y: screenHeight * 0.67, width: screenWidth, height: screenHeight * 0.33)
}

如果有帮助,这是我的键盘视图的图片

在此处输入图像描述

这是我尝试分配时的键盘图片bibleKeyboardView.frame

在此处输入图像描述

任何帮助将非常感激!在过去的几天里,这让我很难过。

标签: swiftuiviewios-keyboard-extension

解决方案


推荐阅读