【问题标题】:Header of viewcontroller doesn't fit视图控制器的标题不适合
【发布时间】:2018-10-26 17:22:38
【问题描述】:

所以我继承的应用程序的原始设置具有这样设置的导航栏(这是在我的 AppDelegate 中):

private func configureNavigationController(_ navigationController: UINavigationController) {
    navigationController.navigationBar.isTranslucent = false

    self.window?.rootViewController = navigationController

    let imageView = UIImageView(image: UIImage(named: "logo-white"))
    imageView.contentMode = UIViewContentMode.scaleAspectFit

    let center = (navigationController.topViewController?.view.frame.width)! / 2.0 - 44

    let titleView = UIView(frame: CGRect(x: center, y: 0, width: 88, height: 44))
    imageView.frame = titleView.bounds
    titleView.addSubview(imageView)


    UINavigationBar.appearance().barTintColor = UIColor.navBackground
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().addSubview(titleView)
}

这会在每个视图控制器上正确创建导航栏,图像在中心,但是我有一些新功能需要放在所有内容之上,并且这个徽标文件 - logo-white - 仍然显示在顶部.

这是我想要解决的真正问题 - 所以如果我在下面尝试的解决方案是错误的,请告诉我并告诉我正确的方法。

无论如何,我尝试在我的 AppDelegate 中注释掉上面的代码,并将其放入我需要它的特定视图控制器中

override func viewDidLoad() {
    super.viewDidLoad()

    let imageView = UIImageView(image: UIImage(named: "logo-white"))
    imageView.contentMode = UIViewContentMode.scaleAspectFit

    let center = (navigationController!.topViewController?.view.frame.width)!// / 2.0 - 44

    let titleView = UIView(frame: CGRect(x: center, y: 0, width: 88, height: 44))

    imageView.frame = titleView.bounds
    titleView.addSubview(imageView)

    navigationItem.titleView = imageView

但这不起作用 - 我可以让徽标显示在屏幕的左侧,或者稍微偏离中心,但从不居中。

我猜这是因为该栏有一个后退按钮和一个小设置图标。

那么,我该如何正确地做到这一点呢?

有没有办法让它可以覆盖徽标?将其移动到我的个人视图控制器中的解决方案是什么?

这里是重叠的图片。徽标“Pinyada”根本不应该掩盖这一点 - 这是一个第三方库,应该在一切之上。

【问题讨论】:

  • 设置为titleView时无需设置imageView的frame
  • FWIW 如果您添加屏幕截图,您的问题会更容易理解
  • 当然。我将包括一个。
  • 我对您希望它的外观感到困惑。你能按照你想要的样子模拟一个屏幕截图吗?

标签: ios swift


【解决方案1】:

你可以试试这个:

override func viewDidLoad() {
    super.viewDidLoad()
    let imageView = UIImageView(image: UIImage(named: "logo-white"))
    imageView.contentMode = .scaleAspectFit
    imageView.frame = CGRect(x: 0, y: 0, width: 88, height: 44)
    navigationItem.titleView = imageView
}

如果您有 navigationItem.titleView ,则不需要另一个标题视图。

有时,如果您需要更精确地控制 titleView,您可以添加 customTitleView。在viewController中加入如下代码,就可以得到你想要的了。

let  imageView = UIImageView(image: UIImage(named: "logo-white"))

private func addTitleView(){
    let nbar =   (navigationController?.navigationBar)!
    let width = nbar.frame.width
    imageView.contentMode = UIView.ContentMode.scaleAspectFit
    imageView.frame = CGRect.init(x: (width - 88) / 2.0 , y: 0, width: 88, height: 44)
    nbar.addSubview(imageView)
}

private func removeTitleView(){
     imageView.removeFromSuperview()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    addTitleView()
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    removeTitleView()
}

顺便说一句,UINavigationBar.appearance().addSubview(titleView)

这个方法会导致所有的navigationBar都有相同的titleView,这不是你想要的。

【讨论】:

  • Hrm,不,这仍然会导致图像不居中。它相对于中心偏左。无论我修改什么 CGRect 参数都是如此。
  • 这可能取决于您的图像是什么。可能图像不对称
  • 不是,图片是对称的,刚检查过。这很奇怪。
  • 无论如何我可以...覆盖应用程序委托中设置的这个图像,同时在视图控制器内部?这听起来很乱,但我的印象是,最初写这篇文章的开发者一开始就有点笨拙
  • 是的。你可以开始一个新的故事板。
【解决方案2】:

我想通了。

对我的标题的其他修改之一是占用了太多空间。我把它变小了,图像非常适合。

我将生成标头的所有代码从 AppDelegate 中移出并移到导航协议中,然后将其弹出到每个视图控制器中。

【讨论】:

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