【问题标题】:iOS Custom Keyboard Extension - Change height to fullscreeniOS 自定义键盘扩展 - 将高度更改为全屏
【发布时间】:2022-04-14 05:21:35
【问题描述】:

我有一个 iOS 自定义键盘扩展 (UIInputViewController),我想让它全屏显示。

改变它的高度非常简单:

self.heightConstraint = NSLayoutConstraint(item: self.view!,
                                                   attribute: NSLayoutConstraint.Attribute.height,
                                                   relatedBy: NSLayoutConstraint.Relation.equal,
                                                   toItem: nil,
                                                   attribute: NSLayoutConstraint.Attribute.notAnAttribute,
                                                   multiplier: 0,
                                                   constant: UIScreen.main.bounds.height)

问题在于键盘的框架位于UIKeyboardDockView 的顶部——底部区域包含地球(更换键盘)和麦克风(听写)图标。

现在,我想访问该底部区域,以便将其高度减去UIScreen.main.bounds.height

有人知道这是否可能吗?如果没有,请随时提出任何其他解决方案。

Xcode UI debugger view Simulator screenshot

【问题讨论】:

    标签: ios swift custom-keyboard


    【解决方案1】:

    这对我有用。看起来在viewWillAppear 中调用view.layoutIfNeeded() 很重要。

    import UIKit
    
    class InputVC: UIInputViewController {
        
        var initialHeightConstraint: NSLayoutConstraint?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            guard let view = view else { return }
            
            view.backgroundColor = .red
            
            view.translatesAutoresizingMaskIntoConstraints = false
            
            self.initialHeightConstraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.height)
            self.initialHeightConstraint?.isActive = true
        }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            
            view.layoutIfNeeded()
        }
    }
    
    class ViewController: UIViewController {
        
        var inputVC = InputVC()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = .green
        }
        
        override var canBecomeFirstResponder: Bool {
            true
        }
        
        override var inputAccessoryViewController: UIInputViewController? {
            inputVC
        }
        
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            
            becomeFirstResponder()
        }
    }
    

    我还尝试将它固定到窗口的几个位置,这也有效,但在控制台中出现了约束错误。

    override func didMove(toParent parent: UIViewController?) {
        super.didMove(toParent: parent)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        guard let view = view, let window = view.window else { return }
    
        self.initialHeightConstraint?.isActive = false
        
        NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: window, attribute: .top, multiplier: 1, constant: 0).isActive = true
        
        NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: window, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
        
        NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: window, attribute: .left, multiplier: 1, constant: 0).isActive = true
    
        NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: window, attribute: .right, multiplier: 1, constant: 0).isActive = true
    
        self.view.layoutIfNeeded()
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2015-04-18
      • 2017-01-30
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2016-04-06
      • 2020-10-13
      • 2019-08-23
      相关资源
      最近更新 更多