最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - The navigation animation and UI break after implementing page blocking for TabView on iOS 18 - Stack Overflow

programmeradmin1浏览0评论

I need to prevent users from selecting a different tab in TabView. The solution using onChange no longer works on iOS 18.

Steps to reproduce:

  1. Navigate from HomeDetails → (UI works as expected)
  2. Navigate from HomeDirectory (blocked by onChange) → Details (UI breaks)
  3. Navigate from HomeDirectory (blocked by onChange) → Details (UI breaks) -> Back (Now we can't navigate to Details anymore).
struct TabViewBugView: View {
  enum AppTab {
    case home
    case directory
  }
  
  @State private var selection: AppTab = .home
  
  var body: some View {
    TabView(selection: $selection) {
      NavigationStack {
        NavigationLink {
          VStack {
            Text("Details")
            Spacer()
          }
        } label: {
          Text("Go to Details")
        }
        
        Text("HomeView")
      }
      .tabItem {
        Label("Home", systemImage: "house")
      }
      .tag(AppTab.home)
      
      
      Text("Directory")
        .tabItem {
          Label("Directory", systemImage: "folder")
        }
        .tag(AppTab.directory)
    }
    .onChange(of: selection) { oldValue, newValue in
      print("\(oldValue) -> \(newValue)")
      if newValue == .directory {
        selection = oldValue
      }
    }
  }
}

How can I properly prevent tab selection without breaking the UI?

I need to prevent users from selecting a different tab in TabView. The solution using onChange no longer works on iOS 18.

Steps to reproduce:

  1. Navigate from HomeDetails → (UI works as expected)
  2. Navigate from HomeDirectory (blocked by onChange) → Details (UI breaks)
  3. Navigate from HomeDirectory (blocked by onChange) → Details (UI breaks) -> Back (Now we can't navigate to Details anymore).
struct TabViewBugView: View {
  enum AppTab {
    case home
    case directory
  }
  
  @State private var selection: AppTab = .home
  
  var body: some View {
    TabView(selection: $selection) {
      NavigationStack {
        NavigationLink {
          VStack {
            Text("Details")
            Spacer()
          }
        } label: {
          Text("Go to Details")
        }
        
        Text("HomeView")
      }
      .tabItem {
        Label("Home", systemImage: "house")
      }
      .tag(AppTab.home)
      
      
      Text("Directory")
        .tabItem {
          Label("Directory", systemImage: "folder")
        }
        .tag(AppTab.directory)
    }
    .onChange(of: selection) { oldValue, newValue in
      print("\(oldValue) -> \(newValue)")
      if newValue == .directory {
        selection = oldValue
      }
    }
  }
}

How can I properly prevent tab selection without breaking the UI?

Share Improve this question asked 1 hour ago Danylo HorielovDanylo Horielov 692 bronze badges 2
  • A custom tab bar would be one way to fix, then you can disable any buttons that are supposed to be, well, disabled. See this answer for an example implementation. – Benzy Neez Commented 52 mins ago
  • Btw, Apple's Human Interface Guidelines discourage this type of behaviour: Don’t disable or hide tab bar buttons, even when their content is unavailable. Having tab bar buttons available in some cases but not others makes your app’s interface appear unstable and unpredictable. If a section is empty, explain why its content is unavailable. – Benzy Neez Commented 47 mins ago
Add a comment  | 

1 Answer 1

Reset to default 0

cool. this code veru nice, relli nice

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论