我试图改变设置按钮的颜色为白色,但不能让它改变。

这两种方法我都试过:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

但没有变化,它看起来仍然是这样的:

我怎么把那个按钮变成白色?


当前回答

我在swift 5中使用它,并为我工作

navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")

其他回答

它将通过-(void)viewDidLoad中的这一行来解决:

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;

你应该这样使用:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white

我更喜欢自定义NavigationController而不是设置全局ui,或者放在ViewController中。

这是我的解决方案


class AppNavigationController : UINavigationController {

  override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
  }

  override func viewWillAppear(_ animated: Bool) {

  }

}
extension AppNavigationController : UINavigationControllerDelegate {

  func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    let backButtonItem = UIBarButtonItem(
      title: "   ",
      style: UIBarButtonItem.Style.plain,
      target: nil,
      action: nil)
    backButtonItem.tintColor = UIColor.gray
    viewController.navigationItem.backBarButtonItem = backButtonItem
  }

  func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {

  }

}

此外,如果你使用全局设置ui,如UIBarButtonItem.appearance(),你不需要像EKEventEditViewController,PickerViewController等苹果Api。tintColor = .white

我在swift 5中使用它,并为我工作

navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")

斯威夫特5.3:

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")