先看下效果:
代码:
import SwiftUI
enum NotifyDefine:String {
case NotifyIsHiddenTabView = "is_hidden_tabview"
}
struct ContentView: View {
@State private var isHiddenTabView = false
var body: some View {
VStack {
TabView() {
if isHiddenTabView == false{
first()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "house.fill")
Text("first")
}.tag(0)
second()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("second")
}.tag(1)
third()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image(systemName: "person.crop.circle")
Text("third")
}.tag(3)
}
}.onAppear(){
// tabView是否隐藏通知
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NotifyDefine.NotifyIsHiddenTabView.rawValue), object: nil, queue: .main) { notify in
isHiddenTabView = notify.object as? Bool ?? false
}
}
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct first: View{
@State private var isUpCreate = false
var body: some View{
ZStack{
Button {
isUpCreate = true
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotifyDefine.NotifyIsHiddenTabView.rawValue), object: isUpCreate)
} label: {
Text("first")
}
if isUpCreate{
popUpView {
self.isUpCreate = false
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotifyDefine.NotifyIsHiddenTabView.rawValue), object: isUpCreate)
}.edgesIgnoringSafeArea(.all)
}
}
}
}
struct second: View{
var body: some View{
VStack{
Text("second")
}.background(Color.yellow)
}
}
struct third: View{
var body: some View{
VStack{
Text("third")
}.background(Color.orange)
}
}
struct popUpView: View{
let ScreenW = UIScreen.main.bounds.width
let ScreenH = UIScreen.main.bounds.height
@State var contentH = 400.0
var popView: (() -> Void)?
var body: some View{
VStack{
VStack{
Spacer()
HStack{
Text("popView").frame(width: ScreenW, height: ScreenH - contentH)
}
Spacer()
}.background(Color(hex: 0x000000, alpha: 0.5))
.onTapGesture {
if nil != popView{
popView!()
}
}
VStack{
Spacer()
Text("contentView").frame(width: ScreenW, height: contentH)
Spacer()
}.background(SwiftUI.Color.red)
}
}
func Color(hex: UInt, alpha: Double) -> Color {
return SwiftUI.Color (
red: CGFloat((hex & 0xFF0000) >> 16) / 255.0,
green: CGFloat((hex & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(hex & 0x0000FF) / 255.0,
opacity: alpha
)
}
}
注意:这里只是弹窗跳出是下导航tabView的隐藏,如果是NavigationView跳转后的页面隐藏tabView,只需要把tabView套在NavigationView里面就可以了。