(SwiftUI)如何读取反应视图的几何?
(SwiftUI) How to Read Geometry of a reactive view?
我需要在此聊天气泡视图之上添加一个视图,例如 Instagram 表情符号反应。我们称它为 EmojiView。它需要始终位于聊天气泡的左下角或右下角。但聊天气泡的大小会随着其中文本的多少而变化。所以我需要根据它所在的气泡的大小来定位 EmojiView。
但是我没有明确的方法来读取那个气泡的大小。 GeometryReader 占用了所有可用的 space,在这种情况下这是一个问题。如果我将文本包装在 GeometryReader 中,它会占据所有高度 space,而我没有可靠的高度 space 可以读取。如果我用 .aspectRatio(.fit)
修改 GeometryReader,高度就等于宽度。如果我在任何地方设置 .fixedSize()
,聊天气泡将不再适应文本大小。所以请分享一个可靠的方法来阅读这个几何
请帮忙解决这个问题
没有 GeometryReader,我无法读取几何图形
使用 GeometryReader,我无法获得可靠的高度读数
不需要使用GeometryReader
。
使用 ZStack 并将 EmojiView 对齐到 bottomTrailing
或 bottomLeading
上,具体取决于您选择的底部想展示一下。
ZStack(alignment: .bottomLeading) {
HStack {
Text("I need to add a view on top of this chat bubble view, like an Instagram emoji reaction. Let's call it EmojiView. It will need to always be in the bottom left or right corner of the chat bubble. But the chat bubble's size will vary with how much text is in it. So I need to position EmojiView based on the size of the bubble it is on top of.")
.background(Color.secondary)
.padding()
}
Text("RABBITS")
.padding(8)
.background(Color.blue)
.padding(20)
}
但是我没有明确的方法来读取那个气泡的大小。 GeometryReader 占用了所有可用的 space,在这种情况下这是一个问题。如果我将文本包装在 GeometryReader 中,它会占据所有高度 space,而我没有可靠的高度 space 可以读取。如果我用 .aspectRatio(.fit)
修改 GeometryReader,高度就等于宽度。如果我在任何地方设置 .fixedSize()
,聊天气泡将不再适应文本大小。所以请分享一个可靠的方法来阅读这个几何
请帮忙解决这个问题
没有 GeometryReader,我无法读取几何图形
使用 GeometryReader,我无法获得可靠的高度读数
不需要使用GeometryReader
。
使用 ZStack 并将 EmojiView 对齐到 bottomTrailing
或 bottomLeading
上,具体取决于您选择的底部想展示一下。
ZStack(alignment: .bottomLeading) {
HStack {
Text("I need to add a view on top of this chat bubble view, like an Instagram emoji reaction. Let's call it EmojiView. It will need to always be in the bottom left or right corner of the chat bubble. But the chat bubble's size will vary with how much text is in it. So I need to position EmojiView based on the size of the bubble it is on top of.")
.background(Color.secondary)
.padding()
}
Text("RABBITS")
.padding(8)
.background(Color.blue)
.padding(20)
}