在 Swift 上获取 UIView 和 UIImageView 的像素和点
Get pixels and points for UIView and UIImageView on Swift
我有一个 ViewController,带有一个名为 MasterView 的视图(这是 ViewController 的第一个 UIView)
在MasterView里面,我有一个UIImageView,两个view的宽度都是320,看下图:
但是当我尝试以编程方式获取图像大小时,我使用以下代码:
/* imagemPrincipalCabeca is an IBOutlet connected to the UIImageView */
print("master uiview frame: \(view.frame.width)")
print("master uiview bounds: \(view.bounds.width)")
print("subview uiimageview frame: \(imagemPrincipalCabeca.frame.width)")
print("subview uiimageview bounds: \(imagemPrincipalCabeca.bounds.width)")
我在控制台上收到以下文本:
master uiview frame: 375.0
master uiview bounds: 375.0
subview uiimageview frame: 320.0
subview uiimageview bounds: 320.0
如果我的代码中的 UIView 宽度和 UIImageView 是 320。为什么 view.frame.width 结果是 375?
我知道以像素为单位的设备屏幕是 375,屏幕中的设备点是 320,但是为什么我在使用上面的代码时得到不同的值?
基于@SWeeper 评论。
我测试了 viewDidAppear
方法和 viewDidLayoutSubviews
方法。
viewDidAppear
方法对我不起作用,
但是 viewDidLayoutSubviews
方法对我有用。
我使用 ContainerView
,也许这就是为什么我需要使用 viewDidLayoutSubviews
方法让我的代码获得正确的视图宽度的原因。
我有一个 ViewController,带有一个名为 MasterView 的视图(这是 ViewController 的第一个 UIView)
在MasterView里面,我有一个UIImageView,两个view的宽度都是320,看下图:
但是当我尝试以编程方式获取图像大小时,我使用以下代码:
/* imagemPrincipalCabeca is an IBOutlet connected to the UIImageView */
print("master uiview frame: \(view.frame.width)")
print("master uiview bounds: \(view.bounds.width)")
print("subview uiimageview frame: \(imagemPrincipalCabeca.frame.width)")
print("subview uiimageview bounds: \(imagemPrincipalCabeca.bounds.width)")
我在控制台上收到以下文本:
master uiview frame: 375.0
master uiview bounds: 375.0
subview uiimageview frame: 320.0
subview uiimageview bounds: 320.0
如果我的代码中的 UIView 宽度和 UIImageView 是 320。为什么 view.frame.width 结果是 375?
我知道以像素为单位的设备屏幕是 375,屏幕中的设备点是 320,但是为什么我在使用上面的代码时得到不同的值?
基于@SWeeper 评论。
我测试了 viewDidAppear
方法和 viewDidLayoutSubviews
方法。
viewDidAppear
方法对我不起作用,
但是 viewDidLayoutSubviews
方法对我有用。
我使用 ContainerView
,也许这就是为什么我需要使用 viewDidLayoutSubviews
方法让我的代码获得正确的视图宽度的原因。