You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI应用打开外部链接返回后导航栏重复问题求助

SwiftUI 应用从外部链接返回后导航栏重复显示问题

在SwiftUI应用中,点击DetailView内的标题按钮打开默认浏览器中的URL,返回应用后导航栏的所有按钮会出现重复显示的情况。该问题在iPhone 11真机和模拟器上均会出现,且在iOS14、15、16版本中稳定复现。

DetailView 简化代码

struct DetailView: View {
let item: RSSItem

var body: some View {
    ScrollView {
        VStack(alignment: .leading) {
            Button(action: {
                openURL(item.url)
            }) {
                Text(item.title)
                    .font(.title)
                    .padding(.bottom)
            }
            
            Text(item.text)
                .font(.body)
            
            // More content...
        }
        .padding()
    }
    .navigationBarTitle(item.title, displayMode: .inline)
}

func openURL(_ urlString: String) {
    guard let url = URL(string: urlString) else {
        print("Invalid URL: \(urlString)")
        return
    }
    
    UIApplication.shared.open(url)
}
}

父视图 FirestoreDataView 导航栏配置代码

struct FirestoreDataView: View {
// ... other state variables and properties

var body: some View {
    NavigationView {
        List {
            // ... list content
        }
        .listStyle(PlainListStyle())
        .navigationBarItems(leading: leadingBarItems, trailing: trailingBarItems)
        .navigationBarTitleDisplayMode(.inline)
    }
    .background(Color.white.edgesIgnoringSafeArea(.all))
    .onAppear {
        fetchData()
        customizeNavigationBar()
    }
}

@ViewBuilder
private var leadingBarItems: some View {
    HStack {
        Button("All Read") {
            markAllAsRead()
        }
        
        Button(showUnreadOnly ? "Unread" : "All") {
            showUnreadOnly.toggle()
        }
    }
}

@ViewBuilder
private var trailingBarItems: some View {
    HStack {
        Toggle("KI Only", isOn: $showKIOnly)
            .toggleStyle(CheckboxToggleStyle())
        
        Toggle("Gut", isOn: $showGutOnly)
            .toggleStyle(CheckboxToggleStyle())
        
        Picker("Category", selection: $selectedCategory) {
            Text("All").tag("")
            ForEach(categories, id: \.self) { category in
                Text(category).tag(category)
            }
        }
    }
}

func customizeNavigationBar() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground()
    appearance.backgroundColor = .clear
    appearance.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 16)]
    let desiredHeight: CGFloat = 44.0 / 3.0 // Adjust the height as needed
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().compactAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    UINavigationBar.appearance().frame.size.height = desiredHeight
}
}

已尝试的解决方案(均无效)

  • 将navigationBarTitle修饰符从VStack移至ScrollView
  • 用NavigationView包裹ScrollView并添加navigationBarTitle修饰符
  • 替换ScrollView为ZStack并添加navigationBarTitle修饰符
  • 实现自定义SafariViewController在应用内打开链接
  • 为主NavigationView添加.navigationViewStyle(StackNavigationViewStyle())
  • 使用WKWebView替代UIApplication.shared.open(url)在应用内打开链接
  • 移除customizeNavigationBar()函数排查自定义样式影响

附加信息

  • 应用使用Firebase Firestore进行数据存储与获取
  • 清理编译文件夹并重启Xcode后问题仍存在

恳请各位提供解决方案或相关思路,感谢支持!


内容的提问来源于stack exchange,提问作者Kate Throw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:53:22