programing

탐색 모음에서 뒤로 단추 색 변경

starjava 2023. 5. 7. 10:45
반응형

탐색 모음에서 뒤로 단추 색 변경

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

다음 두 가지를 모두 사용해 보았습니다.

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
 }

스위프트 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)

이렇게 사용하시면 됩니다. 안에 .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

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.black // to change the all text color in navigation bar or navigation 
    self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color

스위프트 3

가장 많이 업데이트된 답변은 Swift 3에 맞지 않습니다.

여기에 이미지 설명 입력

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

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

색상을 변경하려면 UIColor를 변경합니다.원하는 색까지 흰색인.

답 든모응설정답설정▁allUINavigationBar.appearance().tintColor에 있는 Apple합니다.UIAppearance.h.

의 경우 iOS7 파일: iOS7.tintColor자산이 다음으로 이동되었습니다.UIView그리고 이제는 에 설명된 특별한 유전된 행동을 가지고 있습니다.UIView.h프록시와 할 수 , 따라서 " " "는 " " 입니다.tintColor이제 모양 프록시에서 허용되지 않습니다.

사용할 각하고 속성에 Xcode 모와함께사로 주석이 달렸는지 .UI_APPEARANCE_SELECTOR.

따라서 앱 전체에서 모양 프록시를 통해 탐색 모음을 보라색으로, 제목과 버튼을 흰색으로 칠하는 올바른 방법은 다음과 같습니다.

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

:UIBarButtonItem의 하위 클래스가 아닙니다.UIView 히려오NSObject그래서 그것은tintColor된 속이상않습다가 .tintColorUIView.

불하게도행,,UIBarButtonItem.tintColor는 에는 없습니다로 않습니다.UI_APPEARANCE_SELECTOR하지만 제가 보기엔 문서 버그인 것 같습니다. 레이더에서 Apple Engineering의 응답은 지원된다고 말합니다.

다음에서 이 코드 사용AppDelegate 래스클의 , 부내부didFinishLaunchingWithOptions.

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

UINavigationBar.appearance().tintColor = .white

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

이 조각은 마법을 부립니다.빨간색 대신 원하는 대로 변경합니다.

여러 번 시도했지만 작업할 수 없는 경우 다음을 시도할 수 있습니다.

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red

사실, 저는 여러 번 시도했지만, 이 방법이 효과가 있을 것이라는 것을 알았습니다.

이 코드를 사용해 보겠습니다.

 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
}

신속하게 2.0으로 사용되는

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

"설정" 보기 컨트롤러에 이미 뒤로 단추가 있고 "결제 정보" 보기 컨트롤러의 뒤로 단추 색상을 다른 색상으로 변경하려는 경우, 다음과 같이 "설정" 보기 컨트롤러에서 segue를 준비할 수 있습니다.

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               
    }
}

Swift 5 업데이트됨

Back버튼 색상을 전체적으로 선택하면 다음과 같은 기능을 사용할 수 있습니다.

UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color

그런 다음 각 보기 컨트롤러에서 뒤로 단추 배경색을 설정할 필요가 없습니다.이 버튼을 사용하면 다른컨트롤러에서 뒤로 버튼 색상을 설정할 수 없습니다.

그렇지만

보기 컨트롤러에서 뒤로 버튼 색상을 설정하거나 다른 보기 컨트롤러에서 변경해야 하는 경우 위의 방법을 사용하지 마십시오.다음을 사용할 수 있습니다.

let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
                                  .foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
appearance.backgroundColor = .red // Navigation bar background color

self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance

navigationController?.navigationBar.tintColor = .green // Back button color
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

이것은 iOS 9.0+에서 사용할 수 있습니다.

왜 아무도 이걸 언급하지 않았는지 모르겠습니다...하지만 난 당신이 하던 일을 정확히 하고 있었어요. 하지만 효과가 없었어요.그런 다음 코드를 넣었고 모든 것이 작동했습니다.

위의 해결책은 단일 막대 단추 항목을 변경하는 것입니다.코드에 있는 모든 탐색 모음의 색을 변경하려면 다음 대답을 따르십시오.

기본적으로 기적으로다사음클자변경체로를 사용하여 하는 것.appearance()앱에서 해당 보기의 모든 인스턴스를 전체적으로 변경하는 것과 같습니다.자세한 내용은 여기를 참조하십시오.

AppDelegate.swiftdidFinishLaunchingWithOptions 함수에 다음 코드를 추가합니다.

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()]

Swift 2.0의 경우 AppDelegate.swift에서 다음을 사용하여 Navigation-bar 색조변경하려면 제목 텍스트 및 뒤 단추 색조를 변경합니다.

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.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()

글로벌 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

나는 이것을 swift 5에서 사용하고 있고 나를 위해 일했습니다.

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

어떤 이유에서인지 프로그래밍 방식으로 작동하지 않습니다.그러나 스토리보드에 설정하면 매번 작동합니다.

여기에 이미지 설명 입력

이 문제는 -(void)viewDidLoad:에서 다음 라인으로 해결됩니다.

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

이 줄을 추가해야 합니다.

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

Swift 5.3:

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

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

반응형