Post

Replies

Boosts

Views

Activity

How to stop video from playing when switch bottom tabbar view in SwiftUI?
I have a simple app in SwiftUI, and I want to add video player in app like that, everything works, but when I change the bottom tabbar, video still playing, how we can stop the video? Home: struct Home : View {   @State var index = 0   @State var top = 0   @State var data = [     Video(id: 0, player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video4", ofType: "mp4")!)), replay: false),     Video(id: 1, player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "video5", ofType: "mp4")!)), replay: false),   ]      var body: some View{        ZStack{       PlayerScrollView(data: self.$data)       VStack{         HStack(spacing: 15){           Button(action: {            self.top = 0                 }) {            Text("Following")               .foregroundColor(self.top == 0 ? .white : Color.white.opacity(0.45))               .fontWeight(self.top == 0 ? .bold : .none)               .padding(.vertical)           }          Button(action: {           self.top = 1          }) {           Text("For You")               .foregroundColor(self.top == 1 ? .white : Color.white.opacity(0.45))               .fontWeight(self.top == 1 ? .bold : .none)               .padding(.vertical)           } }         HStack{           Spacer()           VStack(spacing: 35){           Button(action: {             }) {             Image("pic")               .renderingMode(.original)               .resizable()               .frame(width: 55, height: 55)               .clipShape(Circle())             }                 Button(action: {             }) {             VStack(spacing: 8){                Image(systemName: "suit.heart.fill")                   .font(.title)                   .foregroundColor(.white)                Text("22K")                   .foregroundColor(.white)               }}              Button(action: {             }) {             VStack(spacing: 8){               Image(systemName: "message.fill")                   .font(.title)                   .foregroundColor(.white)               Text("1021")                   .foregroundColor(.white)               } }           Button(action: {                 }) {               VStack(spacing: 8){               Image(systemName: "arrowshape.turn.up.right.fill")                   .font(.title)                   .foregroundColor(.white)                Text("Share")                   .foregroundColor(.white)               }  } }           .padding(.bottom, 55)           .padding(.trailing)         }        HStack(spacing: 0){          Button(action: {         self.index = 0          }) {  Image("home")             .resizable()             .frame(width: 25, height: 25)             .foregroundColor(self.index == 0 ? .white : Color.white.opacity(0.35))             .padding(.horizontal) }             Spacer(minLength: 0)            Button(action: {               self.index = 1             }) {              Image("search")             .resizable()             .frame(width: 25, height: 25)             .foregroundColor(self.index == 1 ? .white : Color.white.opacity(0.35))             .padding(.horizontal)           }             Spacer(minLength: 0)            Button(action: {                  }) {              Image("upload")             .renderingMode(.original)             .resizable()             .frame(width: 50, height: 35)             .padding(.horizontal)           }             Spacer(minLength: 0)            Button(action: {              self.index = 2               }) {              Image("comment")             .resizable()             .frame(width: 25, height: 25)             .foregroundColor(self.index == 2 ? .white : Color.white.opacity(0.35))             .padding(.horizontal)           }            Spacer(minLength: 0)             Button(action: {              self.index = 3              }) {               Image("profile")             .resizable()             .frame(width: 25, height: 25)             .foregroundColor(self.index == 3 ? .white : Color.white.opacity(0.35))             .padding(.horizontal)           }}         .padding(.horizontal) }          .padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)       .padding(.bottom, (UIApplication.shared.windows.first?.safeAreaInsets.bottom)! + 5)}     .background(Color.black.edgesIgnoringSafeArea(.all))     .edgesIgnoringSafeArea(.all)   }} PlayerView: struct PlayerView : View {   @Binding var data : [Video]   var body: some View{      VStack(spacing: 0){         ForEach(0..<self.data.count){i in           ZStack{             Player(player: self.data[i].player)             .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)             .offset(y: -5)            if self.data[i].replay{               Button(action: {                 self.data[i].replay = false               self.data[i].player.seek(to: .zero)               self.data[i].player.play()                }) {                 Image(systemName: "goforward")               .resizable()               .frame(width: 55, height: 60)               .foregroundColor(.white) }           }}    } }     .onAppear {        self.data[0].player.play()         self.data[0].player.actionAtItemEnd = .none         NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.data[0].player.currentItem, queue: .main) { (_) in  self.data[0].replay = true       } }   }} Player: struct Player : UIViewControllerRepresentable {    var player : AVPlayer    func makeUIViewController(context: Context) -> AVPlayerViewController{       let view = AVPlayerViewController()     view.player = player     view.showsPlaybackControls = false     view.videoGravity = .resizeAspectFill     return view }  func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {      }} class Host : UIHostingController<ContentView>{    override var preferredStatusBarStyle: UIStatusBarStyle{       return .lightContent   }} Video: struct Video : Identifiable {     var id : Int   var player : AVPlayer   var replay : Bool }
0
0
857
Sep ’21
How we can hide bottom tab bar when click the list items in SwiftUI?
I am new in SwiftUI and I try to build new project to learn quickly and I have a SwiftUI project, everything work very well, and when I click the any list items it is open other view, but bottom Tab bar still located in bottom. I want to hide bottom tab bar in there if I click any list items. Is it possible in SwiftUI? Dark_MenuApp: @main struct DarkMenuApp: App { @AppStorage("isDarkMode") private var isDarkMode = false var body: some Scene { WindowGroup { TabView { NavigationView { DarkMenuView() }.tabItem { Image(systemName: "homekit") Text("home") } Text("Liked") .tabItem { Image(systemName: "heart") Text("Liked") } Text("Reels") .tabItem { Image(systemName: "video") Text("Reels") .environment(\.colorScheme, isDarkMode ? .dark : .light) .preferredColorScheme(isDarkMode ? .dark : .light) } Text("Profile") .tabItem { Image(systemName: "person") Text("Profile") } }.accentColor(.primary) } } } DarkMenuView: struct DarkMenuView: View { @AppStorage("isDarkMode") private var isDarkMode = false var body: some View { VStack{ Picker("Mode" , selection: $isDarkMode) { Text("Light") .tag(false) Text("Dark") .tag(true) }.pickerStyle(SegmentedPickerStyle()) .padding() List(0..<5, id: \.self) { num in NavigationLink(destination: Text("\(num)")) { Text("\(num)") } } }.navigationTitle("Dark Menu") } }
1
0
2.0k
Jul ’21