【问题标题】:How to Increase Navigation Bar Height如何增加导航栏高度
【发布时间】:2018-09-14 00:24:33
【问题描述】:

1.如何像 Apple 为 iMessage 应用程序所做的那样增加导航栏的高度:

2.点击titleView时如何让导航栏展开成这样:

我尝试创建一个更大的titleView,但它只是被剪裁到默认导航栏高度的范围内。他们是如何做到这一点的?此外,我的视图控制器以编程方式嵌入到导航控制器中。

【问题讨论】:

标签: ios swift uinavigationcontroller uinavigationbar


【解决方案1】:

不确定您是否刚刚将导航嵌入到应用中...或以编程方式进行设置。

您需要将导航栏添加为一种“IBOUlet”,然后根据功能/动作/等修改它的高度。

点击“名称按钮”时说;然后类似于“mainNavBar.heightAnchor.constant = 400”(其中 400 是新高度)。

或者,如果您手动设置,并且对导航栏的高度有自动布局约束,那么您可以将其设置为 IBOutlet;并更轻松地访问它。

如果您发布了一些代码,或者至少发布了您的设置方式,将会有所帮助。

【讨论】:

  • 我的视图控制器在应用程序启动时以编程方式嵌入在导航控制器中,我很确定您无法更改导航栏高度约束常量。
【解决方案2】:

使用此类作为导航栏高度:

   import Foundation
    import UIKit

    class NavigationBar: UINavigationBar {

        //set NavigationBar's height
        //For iphonex, I recommended to set the minimum height to 88 or higher.
        var customHeight : CGFloat = 100

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)


            translatesAutoresizingMaskIntoConstraints = false


        }

        override func sizeThatFits(_ size: CGSize) -> CGSize {

            return CGSize(width: UIScreen.main.bounds.width, height: customHeight)

        }

        override func layoutSubviews() {
            super.layoutSubviews()

            self.tintColor = .black

            frame = CGRect(x: frame.origin.x, y:  0, width: frame.size.width, height: customHeight)

            // title position (statusbar height / 2)
            setTitleVerticalPositionAdjustment(-10, for: UIBarMetrics.default)

            for subview in self.subviews {
                var stringFromClass = NSStringFromClass(subview.classForCoder)
                if stringFromClass.contains("BarBackground") {
                    subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: customHeight)
                    subview.backgroundColor = .yellow

                }

                stringFromClass = NSStringFromClass(subview.classForCoder)
                if stringFromClass.contains("BarContent") {

                    subview.frame = CGRect(x: subview.frame.origin.x, y: 20, width: subview.frame.width, height: customHeight - 20)


                }
            }
        }


    }

【讨论】:

    【解决方案3】:

    使用导航视图代替导航栏或在viewDidLoad 功能上使用自定义视图隐藏导航栏并显示您的自定义视图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 2018-03-01
      • 2017-11-14
      • 1970-01-01
      • 2020-03-15
      相关资源
      最近更新 更多