简体   繁体   English

iOS-针对imageView上按钮的不同屏幕尺寸的自动布局缩放

[英]iOS - autolayout scaling for different screen sizes of buttons over an imageView

I have a parent UIView with a UIImageView and another UIView inside. 我有一个父UIView,里面有一个UIImageView和另一个UIView。 The UIImageView has an image and it's constrained to the same size of the parent UIView. UIImageView有一个图像,并且它被限制为与父UIView相同的大小。 The child UIView is also constrained to the same size of the parent UIView and it has buttons that position above certain parts of the UIImageView (this allows the user to click on the image to trigger an action depending on where the image was clicked). 子UIView也被限制为与父UIView相同的大小,并且它的按钮位于UIImageView某些部分的上方(这允许用户单击图像来触发操作,具体取决于单击图像的位置)。

I've tried to set constraints with AutoLayout by using size classes, but I can't seem to get them to resize accordingly to screen sizes. 我试图通过使用尺寸类来设置AutoLayout约束,但似乎无法根据屏幕尺寸来调整它们的大小。 I've created the fixed constraints on an iPhone 8 screen. 我已经在iPhone 8屏幕上创建了固定约束。 iPhone 8, iPhone X, and iPhone 8 Plus all have width Compact/height Regular, however it only looks right on iPhone 8/X. iPhone 8,iPhone X和iPhone 8 Plus的宽度均为“紧凑/高度常规”,但是仅在iPhone 8 / X上看起来正确。 I also would like the button sizes to scale down for iPhone SE/4s. 我也希望按比例缩小iPhone SE / 4s的尺寸。

Here is what it looks like (blue blackground has been replaced for the real image): notice the buttons are the same size in all 3 screens 外观如下所示(真实背景已替换为蓝色背景):请注意,所有3个屏幕的按钮大小均相同

parent UIView constraints 父UIView约束

child UIImageView constraints 子UIImageView约束

child UIView constraints 子UIView约束

The buttons inside the child UIView have fixed constraints of top/trailing/leading/bottom according to the iPhone 8/X screen size. 根据iPhone 8 / X的屏幕大小,子UIView中的按钮具有固定的上/下/前/下约束。

How can I get the buttons to resize accordingly for all screen sizes while maintaining the same position over the image that also scales depending on screen sizes? 如何在所有屏幕尺寸上都相应地调整按钮大小,同时在图像上保持相同的位置,该位置也会根据屏幕尺寸进行缩放?

UPDATE: After replacing the inner UIView with a StackView and updating the constraints, with the distribution filled equally it looks like the following structure: 更新:用StackView替换内部UIView并更新约束后,均等地填充分布,它看起来像以下结构:

Replaced UIView with StackView 将UIView替换为StackView

How do I get it to look like this? 我如何使它看起来像这样?

original screen 原始画面

UPDATE 2: 更新2:

Added spacing and centered X/Y of stackView. 增加了stackView的间距和居中X / Y。

updated stackView 更新了stackView

UPDATE 3: The image I have in the imageView has Content Mode in Aspect Fit. 更新3:我在imageView中拥有的图像在Aspect Fit中具有“内容模式”。 How do I scale the buttons in this mode? 如何在此模式下缩放按钮?

First regarding parent view don't give it static height(371) -> instead give a proportional height constraint to main view 首先关于父视图不要给它静态高度(371)->而是给主视图一个比例高度约束

Second regarding inner subviews 关于内部子视图的第二个

1- imageView won't resize as you think as it has leading , Trailing , Top , B constraints 1- imageView不会像您想象的那样调整大小,因为它具有前导,尾随,顶部,B约束

2- same for the view with buttons 2-对于带按钮的视图相同

instead of give those constraints consider giving centerX&Y with proprtional width & height constraints for these subviews to the parent view 而不是给这些约束,请考虑为这些子视图给CenterX&Y提供比例的宽度和高度约束给父视图

Also stackView will fit for these buttons if you don't already use it 如果您还没有使用stackView,它们也将适合这些按钮

You have to use equal width and equal height constraints for buttons to make it adopt size according to the screen size. 您必须对按钮使用相等的宽度和高度限制 ,以使其根据屏幕尺寸采用尺寸。

Place two horizontal views over the image. 在图像上放置两个水平视图。 and make their height constraint equal. 并使它们的高度约束相等。 for reference view below image. 供参考,如下图。 图像顶部和底部视图的高度相等

then place buttons inside those views then set equal width constraints. 然后在这些视图中放置按钮,然后设置相等的宽度约束。 for reference view below image. 供参考,如下图。

在此处输入图片说明

Hope this will help you. 希望这会帮助你。

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

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