在Swift中确定UIView的宽度

Nik*_*las -1 uiview swift

我有一个自动布局UIView,需要知道它的宽度。如何找到Swift中最简单的宽度?

小智 5

您可以通过以下方式找到任何视图的宽度

 let width = yourView.bounds.width
Run Code Online (Sandbox Code Playgroud)

如果您已将宽度约束应用于视图,并且具有该约束的出口,则可以通过它找到它。

let width = yourWidthConstraint.constant
Run Code Online (Sandbox Code Playgroud)