我已经对我的嵌入式UINavigationcontroller进行了以下自定义。我已经在视图的视图控制器中完成了这个操作,它将首先显示出来。
但是我正在尝试清理它,因此我没有在我的UIView类中通过创建一个单独的类来处理这一切。我正在使用的代码
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)
let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)
self.navigationController?.navigationBar.backIndicatorImage = backButton
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonHigh
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
我试图做的是将UInavigationcontroller扩展到像这样的新类
class RSRNavigationController: UINavigationController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)
let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)
self.navigationController?.navigationBar.backIndicatorImage = backButton
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonHigh
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
这不起作用。它会毫无问题地运行,但它实际上不会做任何事情。
我也尝试过以下
class RSRNavigationController: UINavigationBar{
override func draw(_ rect: CGRect) {
super.draw(rect)
self.setBackgroundImage(UIImage(named: "navigation.background"), for: .default)
let backButton = UIImage(named: "Back.button")?.withRenderingMode(.alwaysOriginal)
let backButtonHigh = UIImage(named: "Back.button.highlighted")?.withRenderingMode(.alwaysOriginal)
self.backIndicatorImage = backButton
self.backIndicatorTransitionMaskImage = backButtonHigh
//self.backItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
但是我似乎无法设置后退按钮的标题。
有谁知道如何解决这个问题?
答案 0 :(得分:3)
当你引用navigationController INSIDE你的自定义navigationController类
时,你的问题就在这里而不是做
self.navigationController?.navigationBar.backIndicatorImage = backButton
这样做
self.navigationBar.backIndicatorImage = backButton
原因?
因为当你通过UINavigationController扩展你的自定义类时,它变成了一个navigationController本身。通过执行self.navigationController,您可以告诉您的应用程序查找您的自定义navigationCOntroller所在的navigationController。当然,这样的导航控制器不存在,因为您的自定义navController是应用程序的主要或顶级navController。因此,只需从自定义类中删除self.navigationController部分,您在viewDidLoad中编写的其余代码应该可以正常工作。
如果您还有任何问题,请随时提出
答案 1 :(得分:0)
您不会在导航控制器本身中更改这些属性,而是在嵌入在导航控制器中的viewController中更改它们。
这些自定义必须在视图控制器的父ViewController
中完成。
答案 2 :(得分:0)
如果您只想自定义navigationBar的backButton,可以在子视图控制器中添加此代码:
func setupLeftBarItem() -> Void {
let originalImage = UIImage.init(named: "leftBatItem")?.withRenderingMode(.alwaysOriginal)
let leftBarItem = UIBarButtonItem.init(image: originalImage, style: .done, target: self, action: #selector(leftBarButtonItemDidTouch))
navigationItem.leftBarButtonItem = leftBarItem
}
func leftBarButtonItemDidTouch() -> Void {
_ = navigationController?.popViewController(animated: true)
}