gpt4 book ai didi

iOS:以编程方式创建的 UINavigationController 的导航栏未扩展到安全区域

转载 作者:行者123 更新时间:2023-12-02 18:31:42 25 4
gpt4 key购买 nike

我希望导航栏扩展到以编程方式创建的 UINavigationController 中的安全区域。我正在开发一个项目,他们以编程方式创建初始 View Controller 并在 SceneDelegate 中设置其导航栏:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 
guard let windowScene = (scene as? UIWindowScene) else { return }

window = UIWindow(windowScene: windowScene)
let initialViewController = initViewController()
let navigationCotnroller = UINavigationController(rootViewController: initialViewController)
navigationBarConfiguration(navigationCotnroller)

window?.rootViewController = navigationCotnroller
window?.makeKeyAndVisible()

}

private func initViewController () -> UIViewController {
let view_controller_to_be_returned = DeviceSearchVC()
view_controller_to_be_returned.title = "Devices"
return view_controller_to_be_returned
}

private func navigationBarConfiguration (_ controller: UINavigationController) {
controller.navigationBar.prefersLargeTitles = true
controller.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
controller.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
controller.navigationBar.tintColor = .white
controller.navigationBar.backgroundColor = UIColor.systemBlue
}

看起来像这样: enter image description here

我想让导航栏扩展到安全区域。

我试过这样的:

    extension UIViewController: UINavigationBarDelegate{

func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}

但是没有用

最佳答案

只需将其添加到 navigationBarConfiguration 函数的末尾

        if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.backgroundColor = UIColor.systemBlue
controller.navigationBar.standardAppearance = navBarAppearance
controller.navigationBar.scrollEdgeAppearance = navBarAppearance
} else {
controller.edgesForExtendedLayout = []
}

enter image description here

关于iOS:以编程方式创建的 UINavigationController 的导航栏未扩展到安全区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69321345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com