user2641891
user2641891

Reputation: 193

Basic SwiftUI Pickers throwing critical warning and weird movement on screen change

I have this simple picker.

  1. Can you tell me why every second time I change picker it crashes the application.

  2. Why there is weird movement when I go inside the picker. It goes up about 20px.

Code:

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)
                })
            }
        }
    }
}

}

This is the error

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>>

Upvotes: 0

Views: 208

Answers (1)

lorem ipsum
lorem ipsum

Reputation: 29242

  1. Your selection variable is a Bool when you set it to true. If you change it to an Int it will save the current tag.

  2. Gets fixed when I set the .navigationViewStyle() for the navigation view. I recently had a beta app get rejected because of this. The screen is just blank on iPad if you leave it on the DefaultNavigationViewStyle().

With these 2 changes I had no issues on a device, on the Simulator the selection view does not open a second time. It might be a glitch.

    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())
        }
    }

Upvotes: 2

Related Questions