I have a view with tabs on the bottom, one of the views
has subviews
, to separate the logic visually, I put the tabs of the subview at the top of the view with the following code and it works perfectly:
self.tabbar.frame = CGRect( x: 0,
y: view.safeAreaInsets.top,
width: self.view.frame.size.width,
height: 50)
How do I do this in SwiftUI
?
Press Cmd+N to create a new SwiftUI View, calling it “MainView”. Creating tabs is as easy as putting different views inside an instance of TabView , but in order to add an image and text to the tab bar item of each view we need to use the tabItem() modifier.
The TabView control is a way to display a set of tabs and their respective content.
If we want to hide the TabBar , we just write TabView into NavigationView , making the NavigationView the super-view and the TabView the child-view, which is just opposite to the above View Hierarchy .
In order to do this you could create your tabs view as a container of the individual tabs something like this...
struct TabbedView: View {
@State private var selectedTab: Int = 0
var body: some View {
VStack {
Picker("", selection: $selectedTab) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
switch(selectedTab) {
case 0: FirstTabView()
case 1: SecondTabView()
case 2: ThirdTabView()
}
}
}
}
Doing this, you are conditionally populating the "Tab page" based on the value of the segmented control.
By using @State
and $selectedTab
the segmented control will update the selectedTab value and then re-render the view which will replace the page based on the new value of selectedTab
.
Edit
Switches now work in SwiftUI beta. 👍🏻
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With