skip to Main Content

I created a new SwiftUI file and using xcode, I literally added the default tabview and it is throwing an error. I attempted to reproduce this error by creating a new Xcode project but it is working perfectly. If I delete the TabView code, it builds fine.

import SwiftUI

struct TabView: View {
    
    @State var tabSelection = 0
    var body: some View {
        Text("wowcool")
      
        TabView(selection: .constant(1)) {
            Text("Tab Content 1").tabItem { Text("Tab Label 1") }.tag(1)
            Text("Tab Content 2").tabItem { Text("Tab Label 2") }.tag(2)
        }
    }
}

struct TabView_Previews: PreviewProvider {
    static var previews: some View {
        TabView()
    }
}

Below is what I am getting:

enter image description here

Attempts to fix:

  1. Restarted Xcode
  2. Cleaned Project
  3. Deleted Derived data
  4. Restarted computer

Currently on Xcode 13.3 and macOS 12.3

2

Answers


  1. You are overriding SwiftUI’s TabView. Rename your view to something else.

    Login or Signup to reply.
  2. This is due to type conflicts (you named your custom view same as system one), so

    Solution 1:

    struct MyTabView: View {
    
       ...
            TabView(selection: .constant(1)) {
    
       ...
    }
    

    Solution 2:

    struct TabView: View {
    
       ...
            SwiftUI.TabView(selection: .constant(1)) {
    
       ...
    }
    

    Note: I would recommend solution 1 always and do not use same names as system types.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search