SwiftUI中的约束是什么? [英] What's with Constraints in SwiftUI?

查看:319
本文介绍了SwiftUI中的约束是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SwiftUI 中的约束现在发生了什么? View 类型是否自动适应较大的设备等?或者我们应该怎么做?

What is happening now with constraints in SwiftUI? Do View types adapt automatically for bigger devices etc. or what should we have to do instead?

推荐答案

RIP,约束!



SwiftUI 不使用布局约束。 UIKit 仍然存在,它并不被弃用且功能齐全,因此,如果继续使用经典方法,则可以根据需要使用任意数量的约束。

RIP, Constraints!

SwiftUI doesn't use layout constraints. UIKit is still around, it's not deprecated and fully functional, so if you continue to use the classic approach, you can use as many constraints as you wish.

但是,如果您选择使用 SwiftUI →安息,束缚!

However, if you choose to go with SwiftUI → rest in peace, constraints!

使视图彼此对齐的核心概念是使用堆栈:

The core concept to align views with each other is using stacks:

  • HStack
  • VStack

如果要覆盖视图(即将一个视图放在另一个视图之上),则可以使用a

If you want to overlay views (i.e. put one view on top of another), you can use a

查看 协议本身(所有视图类型都神秘地遵循了该规则)具有称为修饰符的大量功能,您可以使用这些功能来自定义视图的布局。

The View protocol itself (to which all view types mysteriously conform) has tons of functions called modifiers that you can use to customize your view's layout.

以下是一些实现示例的示例与使用约束相比,具有这些修饰符的特定布局:

Here are some examples how you can achieve specific layouts with those modifiers compared to using constraints:

代替

view.widthAnchor.constraint(equalTo: view.heightAnchor, multiplier: 2)

view
    .aspectRatio(2, contentMode: .fit)

SwiftUI 中。

而不是

view2.leadingAnchor.constraint(equalTo: view1.leadingAnchor, constant: 8)

将视图排列在水平堆栈中,并在它们之间添加一个空格,并添加 frame 修饰符以指定其宽度:

in UIKit you could arrange the views in a horizontal stack and add a spacer between them and add the frame modifier to specify its width:

HStack {
    view1
    Spacer()
        .frame(width: 30)
    view2
}



3。宽度相等



这是变得更加复杂的地方。您不再可以指定两个视图的宽度相等。如果它们在同一垂直堆栈中(即,沿垂直线对齐),则很好:只需将 contentMode 设置为 .fill 并通过设置堆栈视图的宽度→完成的任务来控制实际宽度。 ✅但是,如果不是这样(例如,当它们在水平堆栈中时),则必须找到其他方式来表达它。实际的实现将取决于您要描述的具体布局。

3. Equal Widths

This is where it gets more complicated. You can no longer specify that two views have an equal width. If they are in the same vertical stack (i.e. aligned in a vertical line), fine: just set the contentMode to .fill and control the actual width by setting the stack view's width → mission accomplished. ✅ But if they are not (for example, when they are in a horizontal stack), you have to find other ways to express that. The actual implementation will depend on the concrete layout you're trying to describe.

SwiftUI的一般概念是使视图尽可能小并进行组合。这里需要权衡一点:您付出的代价是,在不同视图层次结构中的视图之间的约束变得更加冗长了实现,最终的好处是布局是声明性的,而创建最常见用户界面的代码是大大简化。

The general idea of SwiftUI is to keep views as small as possible and compose them. There's a little trade-off here: You pay the price that "constraints" between views in different view hierarchies get a lot more verbose to implement, the ultimate gain is that the layout is declarative and the code to create the most common user interfaces is dramatically simplified.

自定义视图填充了整个视图默认情况下为可用空间,这意味着最上面的视图会自动填充整个屏幕-不管实际屏幕大小如何。您可以使用修饰符更改该行为。

Custom views fill the entire available space by default, which means that the top most view automatically fills the entire screen – regardless of the actual screen size. You can use modifiers to change that behavior.

这篇关于SwiftUI中的约束是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆