简体   繁体   English

如何在Swift的导航栏顶部显示自定义视图?

[英]How to show a custom view on top of navigation bar in Swift?

So the way I understand navigation bar (navigation item) is that it has three locations you can modify, which is left ( leftBarButtomItem ), middle ( titleView ), and right ( rightBarButtonItem ). 因此,我对导航栏(导航项)的理解方式是,它具有三个可以修改的位置,分别是左( leftBarButtomItem ),中间( titleView )和右( rightBarButtonItem )。

Now what I'm going to achieve is that I want to just add a simple progress bar line at the very bottom of navigation bar, but still inside navigation bar. 现在,我要实现的是,我只想在导航栏的最底部添加一条简单的进度栏行,但仍在导航栏中。 I want to make this like an extension of navigation bar that I can reuse on other screens. 我想使它像导航栏的扩展,可以在其他屏幕上重复使用。 But I want that left, middle, and right "views" are still working like usual. 但是我希望左,中和右“视图”仍然像往常一样工作。 eg I don't want that if I change the title view content manually in other view controller, then the line disappears / stops working for that other view controller. 例如,我不希望如果我在其他视图控制器中手动更改标题视图内容,则该行消失/停止为该其他视图控制器工作。 So this will feel like an independent overlay added on top of navigation bar as subview, separated from leftBarButtonItem , titleView , and rightBarButtonView , sort to speak. 因此,这感觉就像是在导航栏顶部作为子视图添加的独立叠加层,与leftBarButtonItemtitleViewrightBarButtonView分开,可以说出来。

Is it possible to do that in navigation item? 是否可以在导航项目中执行此操作?

This is a example of how to add a Image instead of a Title String 这是一个如何添加图像而不是标题字符串的示例

fileprivate func setupTitle(){

    let logo = UIImage(named: "my_incredible_logo")
    let imageView = UIImageView(image:logo)
    imageView.contentMode = .scaleAspectFit

    self.navigationItem.titleView = imageView
    self.navigationController?.navigationBar.prefersLargeTitles = false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM