简体   繁体   English

如果设置了锚点,如何获取视图的高度

[英]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.heightself.frame.size.heightself.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.

相关问题 如何创建一个可以动态更改其高度以适合其内容的表格视图单元格? - How can I create a table view cell that changes its height dynamically to fit its content? 如何在swift中旋转屏幕后获得视图的高度? - How can I get the height of a view after screen rotation in swift? 如何防止XIB设置视图的高度? - How do I prevent a view's height from being set by its XIB? 在iOS中使用自动布局,如何将标签放置在其包含视图的高度的1/3处? - Using auto layout in iOS, how can I position a label at 1/3 of the height of its containing view? 如何设置 UITextView 的高度以完全匹配其截断的内容? - How can I set the height of a UITextView to exactly match its truncated content? 我可以使用ARKit和ARSK View更改锚点或移动图像吗? - Can I change anchors or move images with ARKit and ARSK View? 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width 如何根据子视图的高度设置堆栈视图的高度? - How to set stack view height according to its subviews height? 如何使视图高度取决于Swift中的内容? - How can I make views height depend on its content in Swift? 如何为UIView容器设置动画以降低其高度? - How can I animate a UIView Container to decrease its height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM