0

以下是我在实际项目中的一个简化项目,以突出问题。

我有一个项目,我在其中添加了一个带有 UIHostingController 的 SwiftUI 视图,顶部的状态栏是透明的。我在滚动 SwiftUI 视图时看到它。

很容易重新创建,使用故事板创建一个新的 iOS 项目,并ViewController使用 NavigationView 将其嵌入到故事板中。

然后将ViewController内容替换为:

import UIKit
import SwiftUI

final class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let hostingController = UIHostingController(rootView: ScrollView { MySwiftUIView() })
        self.addChild(hostingController)
        view.addSubview(hostingController.view)
        hostingController.view.translatesAutoresizingMaskIntoConstraints = false
        hostingController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        hostingController.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        hostingController.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = UIColor.yellow
        
        self.title = "MyTitle"
    }
}

struct MySwiftUIView: View {
    var body: some View {
        ZStack {
            Color.green
            ScrollView {
                VStack {
                    ForEach(0...100, id: \.self) { index in
                        Text("This is line \(index)")
                    }
                }
            }
        }
    }
}

状态栏是透明的,并显示视图的白色背景:

滚动前

当我开始滚动时MySwiftUIView,状态栏是透明的就更加明显了:

滚动后

我四处寻找解决方案,因为我希望状态栏与导航栏具有相同的颜色,并且不在状态栏中显示来自 SwiftUI 视图的内容。但到目前为止,我还没有找到解决方案。

4

2 回答 2

1

尝试改变:

hostingController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

hostingController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true

我不确定,这是您的预期结果,但它确实消除了SwiftUI内容重叠。

于 2022-02-16T15:40:30.157 回答
1

这也是一个技巧:

 func setStatusBar() {
        if #available(iOS 13, *)
        {
            let keyWindow = UIApplication.shared.connectedScenes
                    .filter({$0.activationState == .foregroundActive})
                    .compactMap({$0 as? UIWindowScene})
                    .first?.windows
                    .filter({$0.isKeyWindow}).first
            let statusBar = UIView(frame: (keyWindow?.windowScene?.statusBarManager?.statusBarFrame) ?? CGRect(x: 0, y: 0, width: screenWidth, height: statubarHeight))
            statusBar.backgroundColor = .green
            keyWindow?.addSubview(statusBar)
        } else {
            let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
            if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
                statusBar.backgroundColor = .green
            }
            UIApplication.shared.statusBarStyle = .lightContent
        }
    }
于 2022-02-16T16:00:05.773 回答