NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Displaying a view from an array of views?

Forums > SwiftUI

Hi.

I've posted this question on stack: https://stackoverflow.com/questions/76397553/swiftui-dynamically-chose-view-from-an-array-of-views****

but people on here are more helpful.

Can someone please help?

   

Hi!

As this answer correclty hints

Views don't belong in arrays, you can use an enum with an @ViewBuilder – lorem ipsum 23 mins ago

You can do as follows.

enum Screens {
    case viewA
    case viewB
}

struct ContentView: View {

    let arrayOfViews: [Screens] = [.viewA, .viewB] // etc.
    @State private var selectedIndex = 1

    var body: some View {
        ZStack {
            navigate(to: arrayOfViews[selectedIndex])
            //navigate(to: .viewA) // or this option
        }
    }

    @ViewBuilder
    func navigate(to screen: Screens) -> some View {
        switch screen {
        case .viewA:
            ViewA()
        case .viewB:
            ViewB()
        }
    }
}

struct ViewA: View {
    var body: some View {
        Text("View A")
    }
}

struct ViewB: View {
    var body: some View {
        Text("View B")
    }
}

   

lorem ipsem is an arrogant dick that instantly downvotes EVERY question I ask on stack. He's been trolling me for months.

I solved it by wrapping them like this:

internal  let arrayOfViews    : [AnyView]   = [AnyView(ViewA()), AnyView(ViewB())]

   

Well, of course if that fits your needs, it is absolutely fine to use AnyView. But remember this is type eraser and might not always be useful.

SwiftUI ships with a special view called AnyView, which can be used as a type erased wrapper to enable multiple view types to be returned from a single function or computed property, or to let us reference a view without having to know its underlying type. However, while there are cases in which we might need to use AnyView, it’s often best to avoid it as much as possible. That’s because SwiftUI uses a type-based algorithm to determine when a given view should be redrawn on screen, and since two AnyView-wrapped views will always look completely identical from the type system’s perspective (even if their underlying, wrapped types are different), performing this kind of type erasure significantly reduces SwiftUI’s ability to efficiently update our views.

full article is here https://www.swiftbysundell.com/articles/avoiding-anyview-in-swiftui/

   

Yeah, people seem overly attached to not using AnyView when even Apple say there is a time and place for it; for me I found it. There are very few hard and fast rules in software engineering, besides Allman bracing being the only good bracing style.

   

To display a view from an array of views, you can follow these steps:

Create an Array of Views: Declare an array to hold your views. For example: swift Copy code var viewsArray: [UIView] = [] Add Views to the Array: Add your views to the array. For example: swift Copy code let view1 = UIView() let view2 = UIView() let view3 = UIView()

viewsArray = [view1, view2, view3] Choose the View to Display: Determine which view from the array you want to display. For example, you can choose the first view: swift Copy code let viewToDisplay = viewsArray.first Add the View to the Parent View: Add the selected view to the parent view where you want it to be displayed. For example, assuming you have a parent view called parentView: swift Copy code parentView.addSubview(viewToDisplay) Configure Layout: Configure the layout constraints or frame of the view to ensure it is properly positioned and sized within the parent view.

Update Display: If you need to change the displayed view dynamically, you can repeat steps 3-5 by selecting a different view from the array and adding it to the parent view or removing the current view from the parent view apk for rts before adding the new view.

By following these steps, you can display views from an array of views dynamically within your parent view.

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.