[英]How can I get the height of a view if I set its anchors
I am currently trying to access the height of a view which I previously set anchors for. 我目前正在尝试访问先前为其设置锚点的视图的高度。 So for example, I set the left, right, top, and bottom anchors of searchTable
using the following in my ViewController: 因此,例如,我在ViewController中使用以下命令设置了searchTable
的左,右,上和下锚:
searchTable.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
searchTable.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
searchTable.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
searchTable.bottomAnchor.constraint(equalTo: menuBar.topAnchor).isActive = true
searchTable
is an object of a class that I created that inherits from UIView and has a UIImageView in it. searchTable
是我创建的类的对象,该类继承自UIView并具有UIImageView。 I constrain the UIImageView by using the following in the init function: 我通过在init函数中使用以下内容来约束UIImageView:
imageView.topAnchor.constraint(equalTo: topContainer.topAnchor, constant: 10).isActive = true
imageView.leftAnchor.constraint(equalTo: topContainer.leftAnchor, constant: 10).isActive = true
imageView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true
imageView.widthAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true
where: 哪里:
let topContainerMultipler: Double = 1 / 7
let imageProfileMultipler: Double = 1 / 5
Inside the init function of searchTable
, I try want to be able to set the corner radius to be half the image size. 在searchTable
的init函数中,我试图将角半径设置为图像大小的一半。 I tried to use self.frame.height
, self.frame.size.height
, self.bounds.height
and also getting the .constant
value of the self.heightAnchor
constraint, but all returns 0. 我尝试使用self.frame.height
, self.frame.size.height
, self.bounds.height
并且还获取了self.heightAnchor
约束的.constant
值,但所有结果都返回0。
I would think that there is a solution to get around this, but I haven't been able to find it. 我认为有解决此问题的解决方案,但我一直找不到。
You are querying the height of your view's frame after defining your constraints, but before layout has actually resolved those constraints into a frame rectangle. 在定义约束之后,但在布局实际将这些约束解析为框架矩形之前,您要查询视图框架的高度。 One approach would be to call layoutIfNeeded
to force layout to happen immediately, then use view.frame.size.height
. 一种方法是调用layoutIfNeeded
强制立即进行布局,然后使用view.frame.size.height
。 The init method is probably not an appropriate place to do this, as opposed to perhaps inside viewDidLoad
on the controller. 与可能在控制器的viewDidLoad
内部相反,init方法可能不适合执行此操作。 Or you might do this in viewDidLayoutSubviews
to recalculate the corner radius every time your view's layout is updated (in which case you won't need layoutIfNeeded
). 或者,您可以在viewDidLayoutSubviews
执行此操作,以在每次更新视图的布局时重新计算拐角半径(在这种情况下,您不需要layoutIfNeeded
)。
Don't use a height constraint if you already use top, bottom, leading, and trailing. 如果已经使用了顶部,底部,前导和尾随,则不要使用高度限制。
Try to visually debug your view by adding a background color, or using the debug view hierarchy button. 尝试通过添加背景色或使用调试视图层次结构按钮来直观地调试视图。
To get the height of a view you should use view.frame.size.height
要获取视图的高度,应使用view.frame.size.height
EDIT (OP question edited) 编辑 (OP问题已编辑)
Your problem is that you try to access the object height when it's just initialized but not displayed yet. 您的问题是您尝试在对象高度刚刚初始化但尚未显示时访问它。 You should create a function that update your UI and call it after your view is loaded. 您应该创建一个函数来更新您的UI并在加载视图后调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.