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

这两种方法我都试过:

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

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

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


当前回答

在Swift3中,将后退按钮设置为红色。

self.navigationController?.navigationBar.tintColor = UIColor.red

其他回答

在Swift 4中,你可以使用以下方法来解决这个问题:

let navStyles = UINavigationBar.appearance()
// This will set the color of the text for the back buttons.
navStyles.tintColor = .white
// This will set the background color for navBar
navStyles.barTintColor = .black

在swift 2.0使用中

self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();

在AppDelegate类中使用此代码,在didFinishLaunchingWithOptions中。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UINavigationBar.appearance().tintColor = .white

}

斯威夫特5.5

更改完整的应用程序主题

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // Set for app
     UINavigationBar.appearance().tintColor = .white
     return true
}

更改特定控制器

let navController = UINavigationController.init(rootViewController: yourViewController) 
navController.navigationBar.tintColor = .red

present(navController, animated: true, completion: nil)

我更喜欢自定义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