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

满月

我希望导航栏扩展到以编程方式创建的 UINavigationController 中的安全区域。我正在开发一个项目,他们以编程方式创建初始视图控制器并在 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
}

它看起来像这样: 在此处输入图片说明

我希望导航栏扩展到安全区域。

我试过这样的事情:

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

但这没有用

法哈德·巴格扎德

只需将此添加到您的navigationBarConfigurationfunc的末尾

        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 = []
        }

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

以编程方式创建的UINavigationController中未显示iOS 14导航栏标题

在iOS中以编程方式创建UINavigationController

iOS-如何以编程方式在安全区域内创建视图

以编程方式启用安全区域

扩展iOS 11安全区域以包括键盘

以编程方式推送UINavigationController后,顶栏不可见

如何以编程方式使用“安全区域布局”?

如何以编程方式添加安全区域

目标C:如何以编程方式在安全区域内创建self.view

带有大标题的以编程方式创建的UINavigationController属于UIStatusBar

Swift 3 尝试以编程方式创建和使用 UINavigationController

iOS 11 iPhone X工具栏不遵守安全区域

隐藏状态栏iOS 11时,安全区域插图会更改

制作新的UINavigationController,然后以编程方式清空旧的导航控制器堆栈

使用下移来关闭UINavigationController会相对于顶部安全区域调整UINavigationBar的大小

如何在 Swift 中以编程方式从 UIView 中删除安全区域?

以编程方式相对于安全区域底部放置视图

iOS-以编程方式创建Today小部件扩展

如何以编程方式打开我的UINavigationController?

如何在IOS 11之前以编程方式获取安全区的高度?

将最低部署目标升级到iOS 9后,无法使用Xcode 9中的安全区域

约束到 UINavigationController 导航栏锚点

如何使导航栏符合安全区域?

UINavigationController“导航栏损坏”的问题

没有导航栏的UINavigationController?

部分屏幕区域中类似iOS UINavigationController的行为(2016)

UINavigationController iOS中的UITableViewcontroller

以编程方式创建扩展的UItableViewCell

使用UINavigationController时在IOS 8中删除状态栏