IT story

네비게이션 바에서 뒤로 버튼의 색상 변경

hot-time 2020. 5. 27. 07:38
반응형

네비게이션 바에서 뒤로 버튼의 색상 변경


설정 버튼의 색상을 흰색으로 변경하려고하는데 변경할 수 없습니다.

나는이 두 가지를 모두 시도했다.

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

그러나 변화는 없지만 여전히 다음과 같습니다.

여기에 이미지 설명을 입력하십시오

그 버튼을 흰색으로 만들려면 어떻게합니까?


보드의 빈 공간을 클릭하여 스토리 보드의 전체 색조 색상을 변경하고 오른쪽 도구 모음에서 "파일 관리자 표시"를 선택하면 도구 모음의 하단에 "글로벌 색조"옵션이 표시됩니다.

스토리 보드의 전역 색조 옵션


이 코드는 화살표 색상을 변경합니다

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

이것이 작동하지 않으면 아래 코드를 사용하십시오.

self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()

스위프트 3 노트

UIColor.whiteColor() 비슷하게 단순화되었습니다 UIColor.white

또한 이전에 암시 적으로 많은 옵션이 명시 적으로 변경되었으므로 다음이 필요할 수 있습니다.

self.navigationController?.navigationBar =

스토리 보드에서 매우 쉽게 설정할 수 있습니다.

여기에 이미지 설명을 입력하십시오

여기에 이미지 설명을 입력하십시오


이것을 사용해야합니다 :

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

빠른

 override func viewDidLoad() {
     super.viewDidLoad()

 self.navigationController?.navigationBar.tintColor = UIColor.white
 }

이처럼 사용할 수 있습니다. 안에 넣으십시오 AppDelegate.swift.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

        return true
    }

에서 Swift3 ,하려면 뒤로 단추를하는 설정 red.

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

스위프트 4.2

완전한 앱 테마 변경

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().tintColor = .white

        return true
    }

특정 컨트롤러 변경

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

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

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

self.navigationController?.navigationBar.tintColor = UIColor.redColor()

이 스 니펫은 마법을 수행합니다. redColor 대신 원하는대로 변경하십시오.


스위프트 3

Swift 3에 가장 많이 답한 답변이 맞지 않습니다.

여기에 이미지 설명을 입력하십시오

색상을 변경하는 올바른 코드는 다음과 같습니다.

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

색상을 변경하려면 위의 UIColor.white를 원하는 색상으로 변경하십시오.


AppDelegate클래스 내 에서이 코드를 사용하십시오 didFinishLaunchingWithOptions.

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

UINavigationBar.appearance().tintColor = .white

}

모든 답변 설정 UINavigationBar.appearance().tintColor은의 Apple 문서와 충돌 UIAppearance.h합니다.

Note for iOS7: On iOS7 the tintColor property has moved to UIView, and now has special inherited behavior described in UIView.h. This inherited behavior can conflict with the appearance proxy, and therefore tintColor is now disallowed with the appearance proxy.

In Xcode, you need to command-click on each property you want to use with appearance proxy to inspect the header file and make sure the property is annotated with UI_APPEARANCE_SELECTOR.

So the correct way to color the navigation bar purple and the title and buttons white throughout the app via the appearance proxy is:

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().tintColor = .white

Note that UIBarButtonItem is not a subclass of UIView but rather NSObject. So its tintColor property is not the inherited tintColor from UIView.

Unfortunately, UIBarButtonItem.tintColor is not annotated with UI_APPEARANCE_SELECTOR – but that seems to me a documentation bug. The response from Apple Engineering in this radar states it is supported.


Lets try this code:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor.whiteColor()  // Back buttons and such
    navigationBarAppearace.barTintColor = UIColor.purpleColor()  // Bar's background color
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  // Title's text color

    self.window?.backgroundColor = UIColor.whiteColor()
    return true
}

in swift 2.0 use

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

If you already have the back button in your "Settings" view controller and you want to change the back button color on the "Payment Information" view controller to something else, you can do it inside "Settings" view controller's prepare for segue like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "YourPaymentInformationSegue"
    {
        //Make the back button for "Payment Information" gray:
        self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
    }
}

Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]

For Swift 2.0, To change the Navigation-bar tint color, title text and back button tint color changed by using the following in AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  // Override point for customization after application launch.


    //Navigation bar tint color change

    UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)

    //Back button tint color change

    UINavigationBar.appearance().barStyle = UIBarStyle.Default
    UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)

    //Navigation Menu font tint color change

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent


    return true
}

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

This works for me, iOS 9.0+


Not sure why nobody has mentioned this...but I was doing exactly what you were doing in my viewDidLoad...and it wasn't working. Then I placed my code into viewWillAppear and it all worked.

The above solution is to change a single barbuttonItem. If you want to change the color for every navigationBar in your code then follow this answer.

Basically changing onto the class itself using appearance() is like making a global change on all instances of that view in your app. For more see here


전역 UI를 설정하거나 ViewController에 넣는 것보다 사용자 정의 NavigationController를 선호합니다.

여기 내 해결책이 있습니다


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) {

  }

}

또한 EKEventEditViewController , PickerViewController같은 Apple Api를 엉망으로 만들 필요가 없습니다.UIBarButtonItem.appearance().tintColor = .white


뒤로 버튼을 숨기고 스스로 선택하십시오. 그런 다음 색상을 설정하십시오.

내가 그거 했어:

self.navigationItem.setHidesBackButton(true, animated: true)
let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
self.navigationItem.leftBarButtonItem = backbtn
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()

-(void) viewDidLoad의 다음 줄로 해결됩니다.

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

이 줄을 추가해야합니다

 self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black

참고 URL : https://stackoverflow.com/questions/28733936/change-color-of-back-button-in-navigation-bar

반응형