【发布时间】:2023-03-10 17:19:01
【问题描述】:
我的布局坏了。我在运行时添加了一个布局,我使用以下代码将其锚定到所有 4 个方面:
func anchorAllSides(to parentView:UIView, identifier: String? = nil ) {
self.translatesAutoresizingMaskIntoConstraints = false
let top = self.topAnchor.constraint(equalTo: parentView.topAnchor)
let bottom = self.bottomAnchor.constraint(equalTo: parentView.bottomAnchor)
let left = self.leadingAnchor.constraint(equalTo: parentView.leadingAnchor)
let right = self.trailingAnchor.constraint(equalTo: parentView.trailingAnchor)
if let identifierString = identifier {
top.identifier = "\(identifierString) - top"
bottom.identifier = "\(identifierString) - bottom"
left.identifier = "\(identifierString) - left"
right.identifier = "\(identifierString) - right"
}
NSLayoutConstraint.activate([top, bottom, left, right])
}
在控制台中,我可以看到 2 个视图具有锚定所有 4 个边的约束,但它们的大小也完全不同。
Printing description of $20:
<UIView: 0x7f93084220c0; frame = (0 0; 375 205.333); autoresize = W+H; gestureRecognizers = <NSArray: 0x600001956ac0>; layer = <CALayer: 0x60000176b320>>
Printing description of $21:
<UIView: 0x7f930840ed40; frame = (0 528.667; 375 528.667); autoresize = RM+BM; layer = <CALayer: 0x6000017698e0>>
(lldb) po [0x7f930840ed40 constraints]
<__NSArrayI 0x600000650330>(
<NSLayoutConstraint:0x60000346a760 'drawerControllerView <-> rolePageDrawerView - bottom' UIView:0x7f93084220c0.bottom == UIView:0x7f930840ed40.bottom (active)>,
<NSLayoutConstraint:0x60000346a7b0 'drawerControllerView <-> rolePageDrawerView - left' H:|-(0)-[UIView:0x7f93084220c0] (active, names: '|':UIView:0x7f930840ed40 )>,
<NSLayoutConstraint:0x60000346a800 'drawerControllerView <-> rolePageDrawerView - right' UIView:0x7f93084220c0.trailing == UIView:0x7f930840ed40.trailing (active)>,
<NSLayoutConstraint:0x60000346a710 'drawerControllerView <-> rolePageDrawerView - top' V:|-(0)-[UIView:0x7f93084220c0] (active, names: '|':UIView:0x7f930840ed40 )>
)
通过在 Xcode 中的视图层次调试器中查看视图,在布局视图后打印上述信息。因此,所有这些都是活动的约束,此时应该使它们的大小相等。
如果它们被限制为相等,那么帧大小如何不同?
更新
为了缩小范围,我在父 VC(角色页面 vc)和子 VC(日历 vc)的 layoutSubviews() 函数中使用了断点日志记录。在两者中,我都记录了抽屉父视图的大小。在子 VC 中,我记录了抽屉视图和 self.view 的大小,这是子到抽屉的所有侧面都固定在抽屉上。我还记录了抽屉视图实例,以确保它们相同。这是另一次运行的日志(因此实例地址已从上面的日志中更改):
role page vc drawerSize (width = 375, height = 190.5)
role page vc drawer 0x00007f7ffd516d50
calendar vc drawerSize (width = 375, height = 222.66666666666666)
calendar vc view size (width = 375, height = 222.66666666666666)
calendar vc drawer is 0x00007f7ffd516d50
role page vc drawerSize (width = 375, height = 589.33333333333337)
role page vc drawer 0x00007f7ffd516d50
role page vc drawerSize (width = 375, height = 589.33333333333337)
role page vc drawer 0x00007f7ffd516d50
calendar vc drawerSize (width = 375, height = 205.33333333333334)
calendar vc view size (width = 375, height = 205.33333333333334)
calendar vc drawer is 0x00007f7ffd516d50
如您所见,抽屉实例是相同的。但是,日历 vc 报告的抽屉视图的大小不同,它被插入和固定到其中。日历 vc 认为它的视图和抽屉视图大小相同,但抽屉视图的父 vc 报告的尺寸更大,即显示时的尺寸。
【问题讨论】:
-
具体什么时候打印描述?您能否展示更多代码以及如何设置视图层次结构?
-
@AndréSlotta 我可以展示更多,但它们与我的问题无关。问题是为什么固定在所有 4 个侧面的 2 个视图的大小不同?
-
实际上,当您打印到控制台时,查看视图是否已经布局是相关的。
-
@AndréSlotta - 我知道。这就是为什么我说我在布局视图之后打印到控制台。这就是为什么我不明白它们如何在不破坏约束的情况下具有不同的尺寸。我希望我清楚自己。在视图布局后,我通过在 Xcode 中打开视图层次结构调试器来打印这些尺寸。
标签: ios swift uiviewcontroller autolayout nslayoutconstraint