iOS 13中控制器显示模式时的UINavigationBar高度

iOS 13中控制器显示模式时的UINavigationBar高度,ios,uitableview,modalviewcontroller,ios13,Ios,Uitableview,Modalviewcontroller,Ios13,控制器显示以下代码: let vc = UIViewController() vc.view.backgroundColor = Colors.green let nvc = UINavigationController(rootViewController: vc) self.present(nvc, animated: true, completion: nil) 它表明 但是导航栏的高度很大。应该是这样的 如何像第二张图片上那样设置导航栏的高度?当堆栈不太深时,导航栏的大小实际上是正

控制器显示以下代码:

let vc = UIViewController()
vc.view.backgroundColor = Colors.green
let nvc = UINavigationController(rootViewController: vc)
self.present(nvc, animated: true, completion: nil)
它表明

但是导航栏的高度很大。应该是这样的


如何像第二张图片上那样设置导航栏的高度?

当堆栈不太深时,导航栏的大小实际上是正确的(至少对于iOS 13标准)。对于索引2(及更高版本?)中显示的视图,导航栏变小

要调整导航栏的大小,请执行以下两个步骤:

  • 获取默认高度约束(名称为“UI视图封装布局高度”)。为此,请在导航栏的约束中进行搜索。我厚颜无耻地抄袭了下面的内容,这使它变得更容易

    extension UIView {
        /// Returns the first constraint with the given identifier, if available.
        ///
        /// - Parameter identifier: The constraint identifier.
        func constraintWithIdentifier(_ identifier: String) -> NSLayoutConstraint? {
            return self.constraints.first { $0.identifier == identifier }
        }
    }
    
  • 将常数设置为所需的值

    let existingConstraint = self.navigationController?.navigationBar.constraintWithIdentifier("UIView-Encapsulated-Layout-Height")
    existingConstraint?.constant = 88.0
    self.navigationController?.navigationBar.setNeedsLayout()
    

  • 完成了。

    当堆栈不太深时,导航栏实际上大小合适(至少对于iOS 13标准)。对于索引2(及更高版本?)中显示的视图,导航栏变小

    要调整导航栏的大小,请执行以下两个步骤:

  • 获取默认高度约束(名称为“UI视图封装布局高度”)。为此,请在导航栏的约束中进行搜索。我厚颜无耻地抄袭了下面的内容,这使它变得更容易

    extension UIView {
        /// Returns the first constraint with the given identifier, if available.
        ///
        /// - Parameter identifier: The constraint identifier.
        func constraintWithIdentifier(_ identifier: String) -> NSLayoutConstraint? {
            return self.constraints.first { $0.identifier == identifier }
        }
    }
    
  • 将常数设置为所需的值

    let existingConstraint = self.navigationController?.navigationBar.constraintWithIdentifier("UIView-Encapsulated-Layout-Height")
    existingConstraint?.constant = 88.0
    self.navigationController?.navigationBar.setNeedsLayout()
    
  • 您已经完成了。

    从,这似乎是iOS 13中的一个bug。
    他们提供的解决办法如下:

    override func viewWillAppear(_ animated: Bool) {  
        super.viewWillAppear(animated)  
        if #available(iOS 13.0, *) {  
            navigationController?.navigationBar.setNeedsLayout()  
        }
    }
    
    我已经在我的项目中进行了测试,它解决了这个问题。希望它也能解决您的问题。

    从中,这似乎是iOS 13中的一个bug。
    他们提供的解决办法如下:

    override func viewWillAppear(_ animated: Bool) {  
        super.viewWillAppear(animated)  
        if #available(iOS 13.0, *) {  
            navigationController?.navigationBar.setNeedsLayout()  
        }
    }
    

    我已经在我的项目中进行了测试,它解决了这个问题。希望它也能解决你的问题。

    对我来说,我必须把这些代码放在
    DispatchQueue.main.async{}
    中才能工作。对我来说,我必须把这些代码放在
    DispatchQueue.main.async{}
    中才能工作。