iOS 11:大标题的UINavigationBar高度(模仿Apple Music App)

时间:2018-03-16 07:43:20

标签: ios uinavigationbar ios11

我正在尝试模仿Apple音乐应用程序使用的UINavigationBar外观(日期显示在大标题上方)。

我知道Apple Music App不使用的标准UINavigationBar,但标题视图为UICollectionView

由于标题文字的大小调整功能,我还想使用的标准UINavigationBar

我可以添加custom date label来查看大型title view的层次结构,我的代码如下所示:

    self.title = "Large Title"
    navigationController?.navigationBar.prefersLargeTitles = true

    guard let navigationBar = navigationController?.navigationBar else { return }

    // Create label
    let label = UILabel()
    label.text = "Small Title"

    // Add label to subview hierarchy
    for subview in navigationBar.subviews {
        let stringFromClass = NSStringFromClass(subview.classForCoder)
        if stringFromClass.contains("UINavigationBarLargeTitleView") {
            subview.addSubview(label)
        }
    }

    // Add label constraints
    label.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        label.leftAnchor.constraint(equalTo: navigationBar.leftAnchor, constant: 16.0),
        label.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -50.0),
        label.heightAnchor.constraint(equalToConstant: 20.0),
        label.widthAnchor.constraint(equalToConstant: 200.0)
        ])

custom date label已正确放置,但我无法将UINavigationBar的高度设置为标签的其他height

Large Title

由于 UINavigationBar使用自动布局,因此设置框架将不再像以前的iOS版本一样工作。

系统添加的自动布局约束。默认情况下,它的优先级为1000,因此向标签添加其他约束不会产生任何影响或导致自动布局冲突。

在不滚动的情况下显示“大标题”上方的“小标题”的任何提示?

1 个答案:

答案 0 :(得分:0)

您可以使用UILabel内的UINavigationBarLargeTitleView稍微改变其内嵌并缩小字体大小,这是一个示例:

        // Create label
        labelSubtitle.text = "Small Title"
        labelSubtitle.backgroundColor = UIColor.clear
        labelSubtitle.textColor = UIColor.searchBarTextColor(dark: theme)
        labelSubtitle.font = UIFont.systemFont(ofSize: 14)

        // Add label to subview hierarchy
        for subview in self.navigationController!.navigationBar.subviews {
            let stringFromClass = NSStringFromClass(subview.classForCoder)
            if stringFromClass.contains("UINavigationBarLargeTitleView") {

                let largeSubViews = subview.subviews
                for sbv in largeSubViews
                {
                    if let lbl = sbv as? UILabel
                    {
                        lbl.padding = UIEdgeInsets(top: 4, left: 0, bottom: 0, right: 0)
                        lbl.setNeedsLayout()
                    }
                }
                subview.addSubview(labelSubtitle)
            }
        }

        self.navigationController!.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30, weight: .bold)]

        labelSubtitle.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            labelSubtitle.leftAnchor.constraint(equalTo: self.navigationController!.navigationBar.leftAnchor, constant: 20.0),
            labelSubtitle.bottomAnchor.constraint(equalTo: self.navigationController!.navigationBar.bottomAnchor, constant: -37.0),
            labelSubtitle.heightAnchor.constraint(equalToConstant: 20.0),
            labelSubtitle.widthAnchor.constraint(equalToConstant: 200.0)
            ])

要更改插件,我正在使用此帖子中发布的扩展程序:Adding space/padding to a UILabel

结果如下:

enter image description here