ios - SwiftUI .popover 框架大小不正确

标签 ios swiftui popover swiftui-sheet

在 iOS 16.4 中,我们现在可以在 .popover 中使用 .presentationCompactAdaptation(.none) 在 iOS 上实现真正的弹出框(紧凑屏幕尺寸)。

SomeView()
    .popover(isPresented: $isPopoverOpen) {
        Text("Hello world!")
            .fixedSize(horizontal: false, vertical: true)
            .padding()
            .presentationCompactAdaptation(.none)
    }

这会给我们类似的东西:

enter image description here

太棒了,它按预期工作了!

当弹出窗口中的 Text() 跨越多行时,就会出现问题。由于某种原因,弹出窗口的高度只会增长到一定高度(使用非动态 .body 字体约 3 行)。下面是使用一些 Lorem Ipsum 文本来说明该问题。请注意,由于弹出框高度太短,末端如何被剪掉:

enter image description here

如何使弹出窗口适合 Text() 内容?我可以静态定义高度,但我希望弹出窗口能够完美适合内容。

最佳答案

您可以使用此 approach 获取 Text 元素的高度.

为此,您可以获取文本的高度并设置您收到的高度。 这就是代码的样子

struct ContentLengthPreference: PreferenceKey {
    static var defaultValue: CGFloat { 0 }
    
    static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
        value = nextValue()
    }
}

struct ContentView: View {
    @State private var isPopoverOpen = true
    @State var textHeight: CGFloat = 0 // <-- this
    
    var body: some View {
        Text("Hello, World!")
            .popover(isPresented: $isPopoverOpen) {
                Text("""
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                        """
                )
                .overlay(
                    GeometryReader { proxy in
                        Color
                            .clear
                            .preference(key: ContentLengthPreference.self,
                                        value: proxy.size.height) // <-- this
                    }
                )
                .onPreferenceChange(ContentLengthPreference.self) { value in // <-- this
                    DispatchQueue.main.async {
                        print (value)
                        self.textHeight = value
                    }
                }
                .fixedSize(horizontal: false, vertical: true)
                .frame(height: textHeight)
                .padding()
                .presentationCompactAdaptation(.none)
                
            }
    }
}

带有长文本的弹出框 Long text

带有短文本的弹出框 Short text

祝你编码愉快!

关于ios - SwiftUI .popover 框架大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76742340/

相关文章:

c++ - 将 C++ 字符串转换为 NSString

ios - SwiftUI在FetchRequest中使用带有结构参数的关系谓词

swift - 设置Widget的TimelineProvider刷新间隔

触摸背景时 Swift Popover 关闭

jquery - Bootstrap 弹出窗口 : reload content with ajax

ios - tvOS 上的 UISwipeGestureRecognizer 给我一个 EXC_BAD_ACCESS

ios - 使用 -ObjC/-all_load 链接器标志时未找到符号

java - 套接字异常 : Connection closed by remote host when pushing to producation destination

swift - 参数类型不符合预期类型 'WKScriptMessageHandler'

javascript - 如何以不止一种方式设置 bootstrap popover 的样式?