【发布时间】: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 如果您添加屏幕截图,您的问题会更容易理解
-
当然。我将包括一个。
-
我对您希望它的外观感到困惑。你能按照你想要的样子模拟一个屏幕截图吗?