swift iPhone 14 Pro/Pro Max上状态栏和WebView之间白色

qvk1mo1f  于 2023-04-19  发布在  Swift
关注(0)|答案(2)|浏览(481)

我在我的应用程序里放了一个WebView,你可以从图片中看到,在有缺口的iPhone上一切都很好(我在iOS 15和16上都试过),而在新的14 Pro和Pro Max上则有一条白色出现在状态栏的末尾和WebView的开头之间,我以为可能是模拟器中的bug但我不太确定...有没有什么方法可以隐藏这条线而不干扰其他设备?
下面是我用于WebView和特定选项卡的代码:

import Foundation
import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    
    let url: URL
    
    func makeUIView(context: Context) -> some UIView {
        let webView = WKWebView()
        let request = URLRequest(url: url)
        webView.load(request)
        return webView
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        
    }
}

struct SearchView: View {
    
    let sysLanguage = NSLocale.current.languageCode
    
    var body: some View {
        if sysLanguage == "it" {
            WebView(url: URL(string: "https://www.vivibusso.app/webapp_test/it/search.html")!)
                .navigationBarHidden(true)
        } else {
            WebView(url: URL(string: "https://www.vivibusso.app/webapp_test/it/search.html")!)
                .navigationBarHidden(true)
        }
    }
}

struct SearchView_Previews: PreviewProvider {
    static var previews: some View {
        SearchView()
    }
}

下面是ContentView的代码:

import SwiftUI
import UIKit

struct ContentView: View {
    
    @State private var selected = 0
    
    var body: some View {
        TabView(selection: $selected) {
            NavigationView {
                HomeView()
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 0 ? "house.fill" : "house"))
                Text("Home")
            }.tag(0)
            NavigationView {
                CategoryView()
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 1 ? "text.justify" : "text.justify"))
                Text("Categorie")
            }.tag(1)
            NavigationView {
                GalleryView()
            }
            .edgesIgnoringSafeArea(.all)
            .navigationViewStyle(StackNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 2 ? "photo.fill" : "photo"))
                Text("Galleria")
            }.tag(2)
            NavigationView {
                SearchView()
            }
            .edgesIgnoringSafeArea(.all)
            .navigationViewStyle(StackNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 3 ? "magnifyingglass" : "magnifyingglass"))
                Text("Ricerca")
            }.tag(3)
        }
        .accentColor(.white)
        .onAppear() {
            UITabBar.appearance().barTintColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
            UITabBar.appearance().backgroundColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
            UITabBar.appearance().unselectedItemTintColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 1.0)
        }
    }
}

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        let app = UIApplication.shared.connectedScenes.first as? UIWindowScene
        let statusBarHeight = app?.statusBarManager?.statusBarFrame.height ?? 0
        let statusbarView = UIView()
        statusbarView.backgroundColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
        view.addSubview(statusbarView)
        statusbarView.translatesAutoresizingMaskIntoConstraints = false
        statusbarView.heightAnchor
            .constraint(equalToConstant: statusBarHeight).isActive = true
        statusbarView.widthAnchor
            .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
        statusbarView.topAnchor
            .constraint(equalTo: view.topAnchor).isActive = true
        statusbarView.centerXAnchor
            .constraint(equalTo: view.centerXAnchor).isActive = true
        let appearanceNav = UINavigationBarAppearance()
        appearanceNav.backgroundColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
        appearanceNav.largeTitleTextAttributes.updateValue(UIColor.white, forKey: NSAttributedString.Key.foregroundColor)
        appearanceNav.titleTextAttributes.updateValue(UIColor.white, forKey: NSAttributedString.Key.foregroundColor)
        appearanceNav.shadowColor = nil
        navigationBar.standardAppearance = appearanceNav
        navigationBar.compactAppearance = appearanceNav
        navigationBar.scrollEdgeAppearance = appearanceNav
        navigationBar.tintColor = UIColor.white
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

谢谢大家!

vawmfj5a

vawmfj5a1#

heightAnchor、topAnchor和centerXAnchor可能相互冲突。请使用widthAnchor、topAnchor和bottomAnchor。

statusbarView.widthAnchor
    .constraint(equalTo: view.widthAnchor).isActive = true
statusbarView.topAnchor
    .constraint(equalTo: view.topAnchor).isActive = true
statusbarView.bottomAnchor
    .constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
kr98yfug

kr98yfug2#

**更新:**仅适用于iOS 15及以上版本(我目前使用的是iOS 16)

我发现将视图偏移到安全区域顶部以下会导致其背景颜色扩展到屏幕顶部。这样你就不必在导航视图中 Package 标签视图项目,并且可以摆脱UINavigationController扩展。请参阅下面我的代码版本中的最后两行。
我不太确定为什么会这样,可能是利用了SwiftUI中的一个bug,将来可能会发生变化。

struct ContentView: View {
    
    @State private var selected = 0
    
    var body: some View {
        TabView(selection: $selected) {
            Text("Home View")
                .tabItem {
                    Image(systemName: (selected == 0 ? "house.fill" : "house"))
                    Text("Home")
                }.tag(0)
            
            Text("Category View")
                .tabItem {
                    Image(systemName: (selected == 1 ? "text.justify" : "text.justify"))
                    Text("Categorie")
                }.tag(1)
            
            Text("Gallery View")
                .tabItem {
                    Image(systemName: (selected == 2 ? "photo.fill" : "photo"))
                    Text("Galleria")
                }.tag(2)
            
            SearchView()
                .tabItem {
                    Image(systemName: (selected == 3 ? "magnifyingglass" : "magnifyingglass"))
                    Text("Ricerca")
                }.tag(3)
        }
        .accentColor(.white)
        .onAppear() {
            UITabBar.appearance().barTintColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
            UITabBar.appearance().backgroundColor = UIColor(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0, alpha: 1.0)
            UITabBar.appearance().unselectedItemTintColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 1.0)
        }
        .offset(CGSize(width: 0, height: 1)) // move TabView down one point
        .background(Color(red: 125.0/255.0, green: 102.0/255.0, blue: 159.0/255.0))
    }
}

相关问题