swiftui - 在 SwiftUI 中对齐视图的高度
问题描述
我有一个 SwiftUI 视图,它是HStack
两个视图中的一个: 1. 一个简单的Text
视图,它充当一种标签,并且始终是单行 2. 一个自定义 UIKit 视图,它UITextView
具有很多自定义格式
我的愿望是限制高度UITextView
以匹配Text
视图的高度(单行,高度取决于系统字体等)。我已经尝试了很多方法来解决这个问题,但我似乎无法找到有效的方法。GeometryReader
似乎只将父属性传递给孩子,但没有解决“兄弟”问题。
有什么想法或见解吗?
解决方案
这是可能布局的演示。使用 Xcode 11.4 / ISO 13.4 测试
注意:添加只是为了演示和更好的可见性.border
。.padding
重要的地方在评论中标注。MultilineTextView
是一个简单的表示UITextView
struct DemoFixedToLabel: View {
var body: some View {
HStack {
Text("Some Text").font(Font.system(.title))
.padding()
.border(Color.blue)
.fixedSize() // << here !!
MultilineTextView(text: .constant("My desire is to restrict the height of the UITextView to match the height of the Text"))
.border(Color.green)
}
.padding()
.border(Color.red)
.fixedSize(horizontal: false, vertical: true) // << here !!
}
}
推荐阅读
- aws-iot - 使用 aws-sdk-go-v2 的 AWS-IOT 工作示例
- graph-theory - 在无向无环图中找到最长回文路径的长度
- string - 如何在 Powershell 中获取字符串的特定部分
- linux - 如何将 Ctrl-C 键发送到正在运行的应用程序而不是禁用 tmux 弹出窗口?
- flutter - 在 Flutter 的本地通知中添加按钮
- node.js - 无法将另一个集合中的对象插入到表单提交时创建的新文档
- laravel - Laravel - 如何更新动态输入字段
- 8thwall-xr - 如何在 Mac 中为 8thwall web ar 编写代码并进行测试?
- python - 如何使用不同的功能来替换列表中的项目?
- c# - 在子视图 XAML 中使用来自父视图的命令