![](/img/trans.png)
[英]Adjusting the frame of a SwiftUI view with a DragGesture resizes it in all dimensions
[英]Get frame/dimensions from SwiftUI view(s)
假设,为了展示一条特定的信息,我有两种不同的自定义 SwiftUI 视图。 举个例子,我的数据是两个字符串,显示它们的选项要么在
HStack { Text() Spacer() Text() }
或者
VStack {
Text()
Text()
}
风格。 为了让 select 成为最合适的一个,我需要渲染它们并根据结果尺寸做出选择。 例如,如果第一种样式中的一个文本视图接近 window 大小的 50%,我宁愿使用第二种样式的 go。
如果用户没有看到临时视图,我将如何 go 关于这个?
我知道GeometryReader
,但我不知道如何将我的“候选视图”渲染到屏幕外,确定大小,然后为我的实际视图层次结构进行选择。
有什么提示吗?
我不知道如何与 SwiftUI 一起正常工作。 但是,以下是使用UIKit
和文本Labels
的方法:
let max_width = UIScreen.main.bounds.width/2
if label.intrinsicContentSize.width > max_width{
//The text would occupy more than 50% of the screen in width
}else{
//The text won't occupy more than 50% of the screen in width
}
您必须在每个Label
上运行之前的 function,然后您可以决定。
我知道这不是一个完整的答案,因为您正在使用 SwiftUI,但有人可能知道如何在那里应用以前的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.