【问题标题】:Why is my layout breaking when I use LocationButton?为什么我使用 LocationButton 时布局会中断?
【发布时间】:2022-06-14 10:31:57
【问题描述】:

CoreLocationUI 中的 LocationButton 似乎在滚动时中断。

这是一个例子:

import SwiftUI
import CoreLocationUI

struct LocationButtonBrokenView: View {
    @State private var txt: String = "Test"
    
    var body: some View {
        List {
            Group {
                Text("Hello, World!")
                VStack {
                    Text("Where are you?")
                    HStack {
                        TextField("Text", text: $txt)
                        LocationButton() {
                            print("Get location")
                        }
                    }
                }
                ForEach(0 ..< 8, id: \.self) {_ in
                    Text("Hello, World!")
                }
            }
            Group {
                ForEach(0 ..< 9, id: \.self) {_ in
                    Text("Hello, World!")
                }
            }
        }
    }
}

struct LocationButtonBrokenView_Previews: PreviewProvider {
    static var previews: some View {
        LocationButtonBrokenView()
    }
}

第一次加载视图时,一切都按预期呈现:

但是当你向下滚动和备份之后它就是垃圾:

【问题讨论】:

  • 我使用 Group 只是为了方便添加足够的项目以强制视图滚动。组与此处的问题无关,并且问题出现在我的实际代码中没有组。
  • 尝试删除 id:\self 并制作自定义子视图而不是组。但在我看来,Vstack 已经失去了它的高度。

标签: ios swift swiftui core-location


【解决方案1】:

在 Reddit 上寻求帮助后,我得出的结论是按钮本身有问题。显然,有关 UITableCellView 的某些事情正在破坏按钮的高度计算。解决方法涉及在列表视图中按钮的最大祖先上设置最小高度。这里有更多信息:https://www.reddit.com/r/SwiftUI/comments/ud650r/locationbutton_breaking_layout/

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 2022-11-29
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多