You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Storyboard开发自定义键盘UI遭遇LayoutConstraints冲突问题求助

自定义键盘Storyboard布局约束冲突解决求助

平台信息

  • Xcode:Version 14.3 (14E222b)
  • 开发语言:Swift

错误日志

2023-04-22 15:44:03.092625+0800 bopomofoKeyboard[1983:45953] Failed to inherit CoreMedia permissions from 870: (null)
2023-04-22 15:44:03.167932+0800 bopomofoKeyboard[1983:45757] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6000010e0870 UIView:0x7fcdf830a9a0.width == 414   (active)>",
    "<NSLayoutConstraint:0x6000010e45f0 'UIView-Encapsulated-Layout-Width' UIView:0x7fcdf830a9a0.width == 393   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000010e0870 UIView:0x7fcdf830a9a0.width == 414   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

关键代码

override func viewDidLoad() {
        // Perform custom UI setup here
        // disable autoresize
        view.translatesAutoresizingMaskIntoConstraints = true
          if(preferredInterfaceOrientationForPresentation.isPortrait)  {
                       //set row landscape protrait height
                        RowA0.heightAnchor.constraint(equalToConstant: 45).isActive = true
                        RowA1.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowA2.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowA3.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowA4.heightAnchor.constraint(equalToConstant: 45).isActive = true
                        RowB1.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowB2.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowB3.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowC1.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        RowC2.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        ThirdKeyboardButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        ShiftButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
                        //change keyboard protrait hight
                         let heightconstraint = NSLayoutConstraint(item: self.view as Any,attribute: .height,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 0.0,
                                                                   constant: 258) // Set custom height here
                                                                   self.view.addConstraint(heightconstraint)
                         //change keyboard protrait width
                          let widthconstraint = NSLayoutConstraint(item: self.view as Any,attribute: .width,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 0.0,
                                                                   constant: 414) // Set custom width here
                                                                   self.view.addConstraint(widthconstraint)
           }
           if(preferredInterfaceOrientationForPresentation.isLandscape) {
                      //set row landscape landscape height
                        RowA0.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowA1.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowA2.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowA3.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowA4.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowB1.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowB2.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowB3.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowC1.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        RowC2.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        ThirdKeyboardButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
                        ShiftButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
                       //change keyboard landscape hight
                         let heightconstraint = NSLayoutConstraint(item: self.view as Any,attribute: .height,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 0.0,
                                                                   constant: 170) // Set custom height here
                                                                   self.view.addConstraint(heightconstraint)
                         //change keyboard landscape width
                          let widthconstraint = NSLayoutConstraint(item: self.view as Any,attribute: .width,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 0.0,
                                                                   constant: 808) // Set custom width here
                                                                   self.view.addConstraint(widthconstraint)
           }
}

Storyboard布局截图

自定义键盘Storyboard截图


解决方案

问题根源

冲突来自你手动添加的固定宽度约束(width == 414/width == 808)与系统自动生成的UIView-Encapsulated-Layout-Width约束冲突——系统会根据设备屏幕尺寸自动给键盘容器设置合适的宽高,强制固定值会和系统约束打架。

具体修复步骤

  1. 移除代码中手动添加的宽高固定约束
    删除viewDidLoad里所有给self.view添加宽高约束的代码,包括竖屏和横屏分支里的self.view.addConstraint(heightconstraint)和self.view.addConstraint(widthconstraint)。

  2. 用正确方式设置键盘高度
    自定义键盘高度需通过重写intrinsicContentSize实现,而非直接给容器加约束:

    override var intrinsicContentSize: CGSize {
        if preferredInterfaceOrientationForPresentation.isPortrait {
            return CGSize(width: UIView.noIntrinsicMetric, height: 258)
        } else {
            return CGSize(width: UIView.noIntrinsicMetric, height: 170)
        }
    }
    
    // 方向变化时刷新高度
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: { _ in
            self.invalidateIntrinsicContentSize()
        })
    }
    
  3. 修正translatesAutoresizingMaskIntoConstraints设置
    将代码里的view.translatesAutoresizingMaskIntoConstraints = true改为false——开启该属性会让系统自动生成约束,和你手动添加的子视图约束冲突。

  4. 优化子视图布局
    保留按钮行的固定高度约束,但确保所有子视图的左右边缘与键盘容器视图对齐,让子视图自动适配系统分配的键盘宽度,无需手动固定容器宽度。

内容的提问来源于stack exchange,提问作者dennis li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:49:54