基本的 SwiftUI 选择器在屏幕变化时发出严重警告和奇怪的动作
Posted
技术标签:
【中文标题】基本的 SwiftUI 选择器在屏幕变化时发出严重警告和奇怪的动作【英文标题】:Basic SwiftUI Pickers throwing critical warning and weird movement on screen change 【发布时间】:2020-01-28 22:24:54 【问题描述】:我有这个简单的选择器。
你能告诉我为什么每次我更改选择器时都会导致应用程序崩溃。
为什么当我进入选择器时会有奇怪的动作。它上升了大约 20 像素。
代码:
struct ContentView: View
@State private var selection = true
var body: some View
NavigationView
Form
Section
Picker(selection: $selection, label:
Text("Picker Name")
, content:
Text("Value 1").tag(0)
Text("Value 2").tag(1)
Text("Value 3").tag(2)
Text("Value 4").tag(3)
)
这是错误
2020-01-28 22:46:03.338431+0000 FlashCards[24878:4518391] [TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView: 0x7fb3b5046e00; baseClass = UITableView; frame = (0 0; 414 852); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000df6fa0>; layer = <CALayer: 0x600000307d20>; contentOffset: 0, -96; contentSize: 414, 205.33333333333337; adjustedContentInset: 96, 0, 83, 0; dataSource: <_TtGC7SwiftUIP13$7fff2c69da4419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___: 0x7fb3b45139a0>>
【问题讨论】:
这能回答你的问题吗? Why is SwiftUI picker in form repositioning after navigation? 这是 NavigationView 行为中的错误。使用 NavigationView 的显示模式作为内联是一种解决方法。 【参考方案1】:当您将其设置为 true 时,您的选择变量是一个 Bool。如果将其更改为 Int,它将保存当前标签。
当我为导航视图设置 .navigationViewStyle() 时得到修复。我最近有一个测试版应用因此被拒绝。如果您将其留在 DefaultNavigationViewStyle() 上,iPad 上的屏幕只是空白。
通过这 2 项更改,我在设备上没有任何问题,在模拟器上,选择视图不会再次打开。这可能是一个小故障。
struct ContentView: View
@State private var selection: Int = 0
var body: some View
NavigationView
Form
Section
Picker(selection: $selection, label:
Text("Picker Name")
, content:
Text("Value 1").tag(0)
Text("Value 2").tag(1)
Text("Value 3").tag(2)
Text("Value 4").tag(3)
)
.navigationViewStyle(StackNavigationViewStyle())
【讨论】:
我认为的 Swiftui 错误以上是关于基本的 SwiftUI 选择器在屏幕变化时发出严重警告和奇怪的动作的主要内容,如果未能解决你的问题,请参考以下文章