iOS 11: Height of UINavigationBar for large title (mimic Apple Music App)

梦想与她 提交于 2019-12-06 04:53:16

问题


I'm trying to mimic the look of UINavigationBar as used by the Apple Music App (the date is shown above the large title).

I know that the Apple Music App doesn't use the standard UINavigationBar of ios11 but a header view is UICollectionView.

I also want to use the standard UINavigationBar of ios11 because of the resizing feature for title text.

I'm able to add the custom date label to view hierarchy of the large title view, my code as shown below:

    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)
        ])

The custom date label is placed correctly but I'm unable to set the height of UINavigationBar to additional height of the label.

Since ios11 UINavigationBar uses Auto-layout, so setting the frame wont work anymore as like the previous iOS versions.

The Auto-layout constraints added by the system. by default it have a priority of 1000, so adding the additional constraints to the label didn't give any effect or result in the Auto-layout conflict.

Any hints to show the "Small Title" above the "Large Title" without scrolling?


回答1:


You can play a little bit with the UILabel inside the UINavigationBarLargeTitleView like change its Insets and reduce font size, here is an example:

        // 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)
            ])

To change the insets I'm using an extension published on in this post: Adding space/padding to a UILabel

The result looks like:



来源:https://stackoverflow.com/questions/49315446/ios-11-height-of-uinavigationbar-for-large-title-mimic-apple-music-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!