简体   繁体   English

如何计算UIImageView大小?

[英]How to calculate UIImageView size?

I have a Swift app which sets a UIImageView to the size of a portion of the screen (set with autoconstraints to the margins and controls above it), and I use the height and width values of the ImageView to create 3 additional UIImageViews programmatically and position them on top of the original image. 我有一个Swift应用程序,该应用程序将UIImageView设置为屏幕一部分的大小(设置自动约束以设置其上的边距和控件),并且我使用ImageView的height和width值以编程方式创建3个额外的UIImageViews和位置它们放在原始图像的顶部。 I am constructing a podium and place three images on top of the podium in appropriate positions. 我正在构造一个讲台,并将三个图像放在讲台上的适当位置。 I then calculate the height and width of each additional image by creating a constant: let conImageHeight = 0.4 * self.imgPodium.frame.height . 然后,我通过创建一个常数来计算每个其他图像的高度和宽度: let conImageHeight = 0.4 * self.imgPodium.frame.height However, I can't get a reliable height and width of the original image. 但是,我无法获得原始图像的可靠高度和宽度。

Initially I used self.imgPodium.bounds.height which worked great for the iPhone 6 simulator screen size, but when I tried running the app on an iPad simulator, it kept the iPhone size values. 最初,我使用self.imgPodium.bounds.height ,它非常适合iPhone 6模拟器的屏幕尺寸,但是当我尝试在iPad模拟器上运行该应用程序时,它保持了iPhone的尺寸值。 The image itself changed size but when I println() the height and width using bounds , I get the values of the iPhone image. 图像本身会更改大小,但是当我使用bounds println()的高度和宽度时,会得到iPhone图像的值。

I then changed and used self.imgPodium.frame.height but this didn't change anything. 然后,我更改并使用了self.imgPodium.frame.height但这没有任何改变。

Is there a better way to get the height and width of a UIImageView (or any control for that matter) after it positions itself through autoconstraints? 通过自动约束定位自身之后,是否有更好的方法来获取UIImageView(或与此相关的任何控件)的高度和宽度?

Thanks. 谢谢。

Questions I've looked at already but didn't help: Change UIImageView size and position programmatically in Swift 我已经看过但没有帮助的问题: 在Swift中以编程方式更改UIImageView的大小和位置
how to check screen size of iphone 4 and iphone 5 programmatically in swift 如何快速以编程方式检查iphone 4和iphone 5的屏幕大小
UIImageView Bounds, Frame UIImageView边界,框架
Cocoa: What's the difference between the frame and the bounds? 可可:框架和边界有什么区别?
How to resize an UIImageView (swift) 如何调整UIImageView的大小(快速)

In viewDidLoad the subviews have been loaded and initialized but not layed out yet. viewDidLoad ,子视图已加载并初始化,但尚未布局。 Therefore their frame property for example is not valid yet and may be the one that was set in Xcode when designing the UI in the first place - that however is not reliable. 因此,例如,它们的frame属性仍然无效,并且可能是在最初设计UI时在Xcode中设置的frame属性-但是这并不可靠。 You should do custom layout in places where you can be sure the layout has already happened yet: eg in viewWillLayoutSubviews or viewDidLayoutSubviews . 您应该在可以确定布局已经发生的地方进行自定义布局:例如,在viewWillLayoutSubviewsviewDidLayoutSubviews Both methods will also get called when the layout somehow needs to change. 当布局需要以某种方式更改时,这两种方法也会被调用。 In viewDidAppear the layout has been done yet and therefore you can do some layouting too. viewDidAppear ,布局已经完成,因此您也可以进行一些布局。 This code however will get not get called when the layout changes due to interface rotation for example. 但是,例如由于界面旋转而导致布局更改时,将不会调用此代码。 AND it will get called after a presented viewController gets dismissed again. 并且在提供的viewController再次被关闭后将被调用。 (not what you would want). (不是您想要的)。

In both cases: do your setup of creating new imageViews in viewDidLoad and do the layout in viewWillLayoutSubviews or viewDidLayoutSubviews ! 在这两种情况下:进行安装以在viewDidLoad中创建新的viewDidLoad并在viewWillLayoutSubviewsviewDidLayoutSubviews进行布局!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM