如何在iOS 11中引入的UINavigationBar的大标题视图上添加自定义视图 [英] How can I add a custom view on the large title view of UINavigationBar introduced in iOS 11

查看:100
本文介绍了如何在iOS 11中引入的UINavigationBar的大标题视图上添加自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在App Store在iOS 11中进行操作时,在UINavigationBar的大标题视图上添加自定义子视图(右侧为用户图标")

I want to add a custom subview on the large title view of UINavigationBar as App Store is doing in iOS 11. ("user icon" on right side)

我们可以通过UINavigationItem.titleView访问传统的导航栏区域,但是似乎没有API可以访问较大的标题视图区域.

We can access the traditional navigation bar area via UINavigationItem.titleView, but it seems that there is no API to access large title view area.

https://developer.apple.com/documentation/uikit/uinavigationitem/ https://developer.apple.com/documentation/uikit/uinavigationbar/

我使用View Hierarchy Debugger确认了名称为"_UINavigationBarLargeTitleView".我可以在其上添加自定义视图吗?

I confirmed the name is "_UINavigationBarLargeTitleView" using View Hierarchy Debugger. Can I add a custom view on it?

推荐答案

解决方案依赖于大标题标签的子视图顺序而不是其私有类名称,以使其符合AppStore准则.

Solution relying on the subview order of the large title label instead of its private class name, to keep it compliant with AppStore guidelines.

class CustomLargeTitleNavigationBar: UINavigationBar {

    override func didMoveToSuperview() {
        super.didMoveToSuperview()

        if #available(iOS 11.0, *) {
            for subview in subviews {
                if let largeTitleLabel = subview.subviews.first(where: { $0 is UILabel }) as? UILabel {
                    let largeTitleView = subview
                    print("largeTitleView:", largeTitleView)
                    print("largeTitleLabel:", largeTitleLabel)
                    // you may customize the largeTitleView and largeTitleLabel here
                    break
                }
            }
        }
    }
}

这篇关于如何在iOS 11中引入的UINavigationBar的大标题视图上添加自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆