【问题标题】:How to add view on top of navigation bar如何在导航栏顶部添加视图
【发布时间】:2019-04-24 11:01:05
【问题描述】:

如何在导航栏上方添加视图? 我有一个自定义导航控制器,我想在导航栏上方显示一个视图(就像在屏幕上一样),所以它应该在其他 ViewControllers 上可见

如果解决方案出现在情节提要上,那就太好了。

尝试在 UIWindow 上添加没有帮助。

【问题讨论】:

  • 为了清楚起见,您能否编辑您的问题:“我有一个自定义导航控制器,并且我想要查看所有内容(就像在屏幕上一样),所以它应该是在其他 ViewController 上可见”不是那么清楚。你是说你想在(?)状态栏或导航栏上展示东西,或两者兼而有之?
  • 我想在导航栏上方显示一个视图,它是否会覆盖状态栏并不重要
  • 在上面放一个uiview,使用渐变得到想要的解决方案
  • self.navigationController.navBar.addSubView(requiredViewToAdd) 在你的 initialViewController 中类似这样的东西

标签: ios swift storyboard


【解决方案1】:

Swift 4.2、Xcode 10+

好的,据我所知(通过您的评论回复,尽管它仍然不是 100% 清楚),您问题的最佳解决方案是使导航栏透明,这样您就可以看到任何 navigationController-在其下方呈现视图控制器。为此,我建议对UIViewController 进行以下扩展:

extension UIViewController {    
    func setupTransparentNavigationBarWithBlackText() {
        setupTransparentNavigationBar()
        //Status bar text and back(item) tint to black
        self.navigationController?.navigationBar.barStyle = .default
        self.navigationController?.navigationBar.tintColor = .black
    }

    func setupTransparentNavigationBarWithWhiteText() {
        setupTransparentNavigationBar()
        //Status bar text and back(item) tint to white
        self.navigationController?.navigationBar.barStyle = .blackTranslucent
        self.navigationController?.navigationBar.tintColor = .white
    }

    func setupTransparentNavigationBar() {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = .clear
        self.navigationController?.navigationBar.isTranslucent = true
    }
}

viewWillAppearUIViewController 子类中使用前两种方法中的任何一种,您都可以根据需要使导航栏完全透明,状态栏文本+ wifi/电池指示器为黑色或白色。然后,您可以通过将约束固定到view.bounds.topAnchor 来显示导航栏 的任何内容。例如。对于带有白色statusBar 文本的透明导航控制器:

class YourViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        setupTransparentNavigationBarWithWhiteText()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多