简体   繁体   English

如何使用自动布局将多个视图与背景图像元素对齐?

[英]How do you align multiple views with background image elements using auto layout?

Here is the scenario: I have a background image with 3 circles and 3 horizontal lines, above the circles I have 3 views in the shape of rectangles. 这是场景:我有一个带有3个圆和3条水平线的背景图像,在这些圆上方,我有3个矩形的视图。 I want the rectangles to be directly above the circles and roughly as wide as the circles as well as the same height across all iPhones and iPads (from the first horizontal bar to the top margin). 我希望这些矩形位于圆的正上方,并且与所有圆大致相同,并且在所有iPhone和iPad上都具有相同的高度(从第一个水平栏到顶部空白)。 I built a sample in Sketch as a visual aid. 我在Sketch中构建了一个示例,作为视觉辅助。 Any help would be much appreciated! 任何帮助将非常感激! visual aid 视觉辅助

The specific issue I'm having with the constraints I've tried is that the top stays pinned to the top margin but the bottom constraint doesn't really scale with the different iPhone sizes. 我尝试过的约束条件存在的一个特定问题是,顶部约束固定在顶部边缘,但是底部约束条件并不能真正随着不同的iPhone尺寸而扩展。 Here is another screen capture. 这是另一个屏幕截图。 bottom height issue The bottom constraint I'm using is a Vertical Space Constraint with the first item as the bottom of the view with an equal relation to the bottom of the rectangle and it's an equal 283 points. 底部高度问题我正在使用的底部约束是垂直空间约束,其中第一项是视图的底部,与矩形的底部具有相等的关系,并且等于283个点。 I understand why it won't work, 283 from the bottom just doesn't end up in the same spot on all 3 devices, but I don't understand how to fix it. 我明白为什么它不会从底层的工作,283只是没有在所有3个器件在同一地点结束了,但我不知道如何解决它。

So basically when you change the height of the rect you need to consider the height of the whole view. 因此,基本上,当您更改矩形的高度时,您需要考虑整个视图的高度。 You would want to have a design height of screen, for instance, I choose 600 here. 您可能希望具有屏幕的设计高度,例如,我在此处选择600。 So I'd use the following function to calculate the actual height I want for the current device. 因此,我将使用以下函数来计算当前设备所需的实际高度。

    func actualHeight(height: CGFloat) -> CGFloat {
        return height / 600 * view.bounds.height
    }

So when you want to set the height of the rect to 100 out of 600, I'd use actualHeight(100) instead. 因此,当您要将rect的高度设置为600中的100时,我会改用actualHeight(100)

Remember if you've already set the height constraint in the interface builder, it wouldn't work by setting the height of the view directly. 请记住,如果您已经在界面构建器中设置了高度限制,则不能通过直接设置视图的高度来起作用。 One way to do is by cooperating with the constaints. 一种方法是与社区合作。 You'd have to create an outlet for the height constraints and change the constant property of the constraint. 您必须为高度约束创建出口,并更改约束的constant属性。

I got this image online, but it shows how to create an outlet for the constraint. 我在线获得了此图像,但它显示了如何为约束创建出口。

So you'd have a IBOutlet : 因此,您将拥有IBOutlet

@IBOutlet weak var heightConstaint: NSLayoutConstraint!

And you'll set the height like this: 然后您将像这样设置高度:

heightConstaint.constant = actualHeight(400)

If you don't like the height constaint method, you can use transform: 如果您不喜欢使用高度约束方法,则可以使用transform方法:

// Scale the height by 1.1.
rect.transform = CGAffineTransformMakeScale(1.0, 1.1)

As long as your height constaint is set correctly in the IB, this will scale it on whatever devices. 只要您在IB中正确设置了高度约束,它将在任何设备上缩放。

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

相关问题 您如何应用自动布局约束将标签与背景图像元素对齐? - What do you apply auto layout constraints to line up labels with background image elements? 如何使用Masonry DSL for Auto Layout定义视图之间的垂直和水平关系? - How do you define the vertical and horizontal relationship between views using the Masonry DSL for Auto Layout? 如何使用自动布局垂直堆叠表视图? - How do I stack table views vertically using auto layout? 自动布局多个视图 - Auto layout of multiple views 如何使用自动布局连续对齐2个按钮? - How align 2 buttons in a row using auto layout? UIButton具有背景图片和标题(使用自动布局) - UIButton with background image and title using auto layout 使用自动布局在通知中心小部件中正确对齐元素 - Properly align elements in Notification Center widget using Auto Layout 如何使用情节提要自动布局调整这两个元素并调整其大小? - How can I align and resize these two elements with Storyboard Auto Layout? 使用自动布局(或其他方法)“放大”多个视图 - Using auto-layout (or another method) to “scale up” multiple views 如何以编程方式使用自动布局调整多个 iOS 屏幕尺寸的视图大小 - How to resize views for multiple iOS screen sizes using auto layout programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM