【问题标题】:iOS custom keyboard changing constraintsiOS 自定义键盘更改约束
【发布时间】:2018-12-04 10:53:40
【问题描述】:

我正在尝试以编程方式快速创建自定义键盘,并且到目前为止能够在 iPhone 上为 portraitlandscape 创建我想要的约束,但只有当我以相应的方向启动键盘时。

当我自己翻转设备时出现问题:PortraitLandscape。我已将我的约束存储在两个数组中,并在我的 viewDidLoad 和 viewWillTransition 函数中运行这段代码:

if UIScreen.main.bounds.height >= 568 {
        NSLayoutConstraint.deactivate(constraintsLandscape)
        NSLayoutConstraint.activate(constraintsPortrait)
}
else if UIScreen.main.bounds.height <= 414 {
        NSLayoutConstraint.deactivate(constraintsPortrait)
        NSLayoutConstraint.activate(constraintsLandscape)
}

对此的任何帮助表示赞赏。

当前portraitlandscape

【问题讨论】:

    标签: ios iphone swift keyboard constraints


    【解决方案1】:

    如果这是在一个方法中,你会在屏幕旋转后调用它吗?如果不这样做,您可以检查设备旋转覆盖此方法:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        if UIDevice.current.orientation.isLandscape {
            print("Device is in Landscape mode")
            NSLayoutConstraint.deactivate(constraintsPortrait)
            NSLayoutConstraint.activate(constraintsLandscape)
        } else {
            print("Device is in Portrait mode")
            NSLayoutConstraint.deactivate(constraintsLandscape)
            NSLayoutConstraint.activate(constraintsPortrait)
        }
    }
    

    如果您已经这样做了,那么您只需在更改约束后调用self.view.layoutifNeeded()

    【讨论】:

    • 我到底在哪里调用 self.view.layoutIfNeeded() 以及我在里面放了什么?而且,当我运行您的代码时,什么也没有打印。不过感谢您的快速回复。
    • 更改约束后添加
    • 看起来还是不太对劲。我已将当前的屏幕截图包含在问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2018-09-01
    • 2016-04-04
    • 2015-04-18
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多