gpt4 book ai didi

ios - 为什么自动布局对此布局有问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:30 24 4
gpt4 key购买 nike

我在所有屏幕尺寸 上的Autolayout width 键盘扩展时遇到了一些问题。

起初它工作正常,但突然开始吐出一些错误,无论屏幕的宽度如何。我没有做任何更改,我什至尝试删除应用程序、清理项目并从头开始布置所有内容。

这是我的布局:

| - 8 - (50) - >=8 - (130) - 8 - (130) - 8 - |
priority (50) = 750, everything else = 1000
  • 在 super View 和按钮 1 之间有一个 8 磅的空间,它的宽度为 50,但优先级为 750
  • 然后在按钮 1 和按钮 2 之间有一个 8 点或更大的空间,其宽度为 130
  • 然后在按钮 2 和按钮 3 之间有一个 8 磅的空间,它的宽度也为 130
  • 最后,按钮 3 和 super View 之间有一个 8 磅的空间

Screenshot of storyboard layout

这不仅在 320 点4 英寸 屏幕上,而且在 375 点 上都会引起问题4.7 英寸 屏幕和414 点5.5 英寸 屏幕。

据我所知,应该没有任何问题,因为项目的最大宽度是 (8+50+8+130+8+130+8) 342 点。这小于 4.7 和 5.5 英寸屏幕。在 4 英寸 上,我通过打破按钮 1 上的 50 点宽度 解决了这个问题,所以总数是 320 点

它在模拟器和设备上看起来都很好,但是控制台吐出许多 Unable to simultaneous satisfy constraints 错误(准确地说是 3 个)。

这是日志:

1: (
"<NSLayoutConstraint:0x1740954a0 H:[UIButton:0x137e0cb80'Last sentence'(130)]>",
"<NSLayoutConstraint:0x1740955e0 H:[UIButton:0x137e0cda0'Pasteboard'(130)]>",
"<NSLayoutConstraint:0x174095810 H:|-(8)-[UIButton:0x137d0ba60] (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x1740958b0 H:[UIButton:0x137d0ba60]-(>=8)-[UIButton:0x137e0cda0'Pasteboard']>",
"<NSLayoutConstraint:0x174095950 H:[UIButton:0x137e0cda0'Pasteboard']-(8)-[UIButton:0x137e0cb80'Last sentence']>",
"<NSLayoutConstraint:0x1740959f0 H:[UIButton:0x137e0cb80'Last sentence']-(8)-| (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x170094ff0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x137e0c1b0(55)]>"
)
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x1740955e0 H:[UIButton:0x137e0cda0'Pasteboard'(130)]>

2: (
"<NSLayoutConstraint:0x1740954a0 H:[UIButton:0x137e0cb80'Last sentence'(130)]>",
"<NSLayoutConstraint:0x174095810 H:|-(8)-[UIButton:0x137d0ba60] (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x1740958b0 H:[UIButton:0x137d0ba60]-(>=8)-[UIButton:0x137e0cda0'Pasteboard']>",
"<NSLayoutConstraint:0x174095950 H:[UIButton:0x137e0cda0'Pasteboard']-(8)-[UIButton:0x137e0cb80'Last sentence']>",
"<NSLayoutConstraint:0x1740959f0 H:[UIButton:0x137e0cb80'Last sentence']-(8)-| (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x170094ff0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x137e0c1b0(55)]>"
)
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x1740954a0 H:[UIButton:0x137e0cb80'Last sentence'(130)]>

3: (
"<NSLayoutConstraint:0x174095810 H:|-(8)-[UIButton:0x137d0ba60] (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x1740958b0 H:[UIButton:0x137d0ba60]-(>=8)-[UIButton:0x137e0cda0'Pasteboard']>",
"<NSLayoutConstraint:0x174095950 H:[UIButton:0x137e0cda0'Pasteboard']-(8)-[UIButton:0x137e0cb80'Last sentence']>",
"<NSLayoutConstraint:0x1740959f0 H:[UIButton:0x137e0cb80'Last sentence']-(8)-| (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x170094ff0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x137e0c1b0(0)]>"
)
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x1740958b0 H:[UIButton:0x137d0ba60]-(>=8)-[UIButton:0x137e0cda0'Pasteboard']>

我整个晚上都在为这个问题挠头。你们有什么建议吗?

最佳答案

无论出于何种原因,父 View 至少暂时只有 55 点宽。从您引用的日志中:

"<NSLayoutConstraint:0x174095810 H:|-(8)-[UIButton:0x137d0ba60]   (Names: '|':UIView:0x137e0c1b0 )>",
...
"<NSLayoutConstraint:0x1740959f0 H:[UIButton:0x137e0cb80'Last sentence']-(8)-| (Names: '|':UIView:0x137e0c1b0 )>",
"<NSLayoutConstraint:0x170094ff0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x137e0c1b0(55)]>"

因此,|( super View 的边缘)是 UIView:0x137e0c1b0 并且宽度限制为 55 点(UIView:0x137e0c1b0(55) ).

该 View 大概由框架控制。您需要适应它具有任意宽度(甚至是 0,如上一个异常(exception))。为此,将其中一个水平间距约束设为非常高但不是必需的优先级,例如 950。这样,如果可能的话,它将保持不变,但如果必须的话,它会被打破而不会导致异常。显而易见的选择是对父 View 后缘的约束。

super View 比屏幕窄的情况大概是在 View 层次结构被放在一起时,然后才显示。因此,此时允许打破约束不会影响任何实际可见的布局。

关于ios - 为什么自动布局对此布局有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31361424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com