UFO ET IT

UINavigationController 뒤로 버튼 이름을 변경하는 방법은 무엇입니까?

ufoet 2020. 12. 13. 10:01
반응형

UINavigationController 뒤로 버튼 이름을 변경하는 방법은 무엇입니까?


나는 a가 UIViewController있고 첫 번째 뷰 컨트롤러에서 두 번째 뷰 컨트롤러 navigationcontroller로 이동 중이며 돌아 가기 위해에 표시된 버튼의 이름을 변경하고 싶습니다 ....

SecondViewController *secondController = [[SecondViewController alloc]
                                              initWithNibName:nil
                                              bundle:NULL]; 
[self.navigationController pushViewController:secondController  animated:YES];

이제 두 번째에 viewcontroller내가있는 버튼의 이름을 변경합니다 navigationController.


viewWillAppear에서 이것을 작성하십시오.

self.navigationItem.title = @"List View";

그리고 ViewWilldisapper에서 이것을 작성하십시오.

self.navigationItem.title = @"Back";

스토리 보드없이 작동합니다.


프로그래밍 방식으로이 작업을 수행하려면 다음과 같이 수행 할 수 있습니다.

목표 -C

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:nil
                                                            action:nil];

[self.navigationItem setBackBarButtonItem:backItem];

빠른

let backItem = UIBarButtonItem(title: "Custom", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

그러나 Interface Builder를 선호하는 경우 뒤로 버튼을 설정하려는 UINavigationItem을 선택하고 속성 관리자로 이동하여 뒤로 버튼의 제목을 변경합니다.

여기에 이미지 설명 입력

참고 : 현재 보이는보기 컨트롤러가 아니라 뒤로 버튼을 탭할 때 돌아갈보기 컨트롤러에서이를 구성해야한다는 점을 기억하는 것이 중요합니다.


Interface Builder 또는 Storyboarding에서 쉽게 할 수있는 방법이 있습니다. 상위 뷰 컨트롤러에서 탐색 항목의 "뒤로 버튼"속성을 원하는 제목으로 설정하기 만하면됩니다. 다음은 그 예입니다.

부모 뷰 컨트롤러에서 자식 뷰 컨트롤러의 뒤로 버튼 제목 설정

Villian의 Tavern View는 부모의 탐색 컨트롤러 (Best Bars)의 속성 관리자에서 방금 설정 한 것처럼 Back Button Title로 "뒤로"를 갖습니다.


제 경우에는 다음과 같이 메인 스레드에서만 코드를 실행했습니다.

dispatch_async(dispatch_get_main_queue(), ^(void) {

    self.navigationController.navigationBar.backItem.title = @"Custom Title";
});

스위프트 3 :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

참고 : 뒤로 버튼은 현재 화면에 표시된 컨트롤러가 아닌 이전보기 컨트롤러에 속합니다. 뒤로 버튼을 수정하려면 segue를 시작한 뷰 컨트롤러에서 푸시하기 전에 업데이트해야합니다.


Swift 에서는 솔루션이 매우 간단하다는 것을 알았습니다.

나는에있어 가정 ViewControllerA에 와에가는 ViewControllerB 난에 표시 뒤로 버튼의 이름을 변경하고자하는 경우, ViewControllerB을 , 나는이 작업을 수행합니다 ViewControllerA에 ,

self.title = "BackButtonTitle"

그게 다야.

참고 : -ViewControllerA의 제목이 변경됩니다.


뷰 컨트롤러에서 "시뮬레이트 된 메트릭"옵션이 "추론 됨"으로 선택되지 않는 것이 매우 중요합니다.

나는 "없음"으로 시도했고 모든 것이 맞다!


다음과 같이 시도하십시오.

NSArray *viewControllerArr = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousViewControllerIndex = [viewControllerArr indexOfObject:self] - 1;
UIViewController *previous;
if (previousViewControllerIndex >= 0) {
    previous = [viewControllerArr objectAtIndex:previousViewControllerIndex];
    previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                 initWithTitle:@"Back"
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:nil];
}

If you use Storyboard references, the View Controller might not display the Navigation Item on Interface Builder.

Manually dragging a Navigation Bar will not work, you need to drag a Navigation Item

You'll then be able to set its back button's title. This is also where you'll set the view's title.

Quoting Mick MacCallum:

NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.


You can achieve this by using titleView of navigationItem

Swift 3 Answer

• Create one extension for UINavigationItem as Below

extension UINavigationItem {
    func customTitle(title: String) {
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = UIColor.white
        titleLabel.textAlignment = .center
        titleLabel.sizeToFit()
        titleView = titleLabel
    }
}

• In your controllers viewDidLoad do the following

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Text For Back Button"
        navigationItem.customTitle(title: "Title for your view controller")
}

Try Below Code :

[self.navigationItem setValue:@[@"customTitle",@""] forKey:@"abbreviatedBackButtonTitles"];

Actually, you can change title of back button:

self.navigationItem.backBarButtonItem.title = @"NewName";

I'm on ViewController1 and going to ViewController2, If I wants to change name of back button showing on ViewController2, I will do this in ViewController2.

Swift 5

self.navigationController?.navigationBar.topItem?.title = "BackButtonTitle"

Objective-C

self.navigationController.navigationBar.topItem.title = @"BackButtonTitle";

If you use Storyboard, there is a easy way to do this (it worked fine for me).

  • Go to your storyboard
  • Select which UIViewController you want to change the back button text.
  • Click on the UINavigationItem on the top of your UIViewController. (the one with the title)
  • On the Right panel, select the attribute inspector (the 4th icon).
  • Now you can set the Title, Prompt and Back Button texts.

여기에 이미지 설명 입력

Source: http://pinkstone.co.uk/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/


There are two ways to do this.

First way is by program code:

Yakimenko Aleksey's answer is the simplest way, This really works ( tested on Xcode 8 + iOS 10)

Swift3:

self.navigationItem.backBarButtonItem?.title = "Your customized back title"

Note: you should call above code in source view controller, not the destination view controller.

Second way is use storyboard localization via Main.strings file,

"nnnnnnn.title" = "localized string";

The nnnnnnn means the object id of the BackBarButtonItem in the source view controller(not the destination view controller!)

You need set the "back" property of the navigation bar item, it will auto create a BarButtonItem, you find its object id.

Sorry, i failed to upload screenshots.


I had a rather complex storyboard with many navigation controllers, and my customer wanted all of the back buttons to say "Back"

I went through in Interface Builder and put "Back" into the Back button field of every UINavigationItem.

I was not working for every scene change!

문제는 스토리 보드의 모든 뷰 컨트롤러에 UINavigationItem이있는 것은 아니라는 것 입니다.

새 UINavigationItem을 하나가없는 모든 UIViewController로 드래그하고 모든 뒤로 버튼 필드를 "뒤로"로 설정하여이 문제를 해결했습니다.

자체 UINavigationItem이 자동으로 제공되지 않는 컨트롤러를주의하십시오.


스위프트 4

당신에서 의 UIViewController 이 설정 :

let backButton = UIBarButtonItem()
backButton.title = "[Your title here]"
self.navigationItem.backBarButtonItem = backButton

참고 URL : https://stackoverflow.com/questions/9871578/how-to-change-the-uinavigationcontroller-back-button-name

반응형