developer tip

탭바 항목 제목과 무관하게 uiviewcontroller 제목을 변경하는 방법

optionbox 2020. 8. 29. 10:45
반응형

탭바 항목 제목과 무관하게 uiviewcontroller 제목을 변경하는 방법


뷰에서 다음과 같이 뷰 컨트롤러 제목을 설정하고 있습니다.

self.title = @"my title";

그 전에 나는 그것이 내장 된 뷰 컨트롤러와 내비게이션 컨트롤러를 위해 스토리 보드에 제목을 설정했습니다. 나는 이것을 "Title"로 설정했습니다.

뷰 컨트롤러가있는 탭을 클릭하면 탭 표시 줄 항목의 제목이 uiviewcontroller다음으로 변경됩니다. 내 제목

보기 컨트롤러를 변경하고 싶지만 탭 표시 줄 항목은 제목 : 제목과 함께 유지됩니다.

어떻게 할 수 있습니까?


탐색 표시 줄의 제목은 변경하고 탭 표시 줄의 제목은 변경하지 않으려는 것 같습니다. 그렇게해야합니다.

[self.navigationItem setTitle:@"my title"];

빠른:

self.navigationItem.title = "My Title"

그래도 이해하지 못하는 사람들을 위해 (나처럼)

self.navigationItem.title = @"my title";탐색 모음 제목을 설정합니다 .

self.tabBarItem.title = @"my title";탭 표시 줄 제목을 설정합니다 .

self.title = @"my title";두 가지 .


빠른

상단 표시 줄 제목 설정

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

탭 항목 제목 설정

self.tabBarController?.tabBar.items?[0].title = "tab title"

두 제목 설정

self.title = "both titles"

들어 스위프트는 이를 사용,

self.navigationItem.title = "Navigation bar title" 
self.title = "Tab bar title"

참고 : 각보기 컨트롤러의 루트에 탐색 컨트롤러가있는 탭 모음 컨트롤러가있는 경우 .NET Framework를 설정하는 경우보기 컨트롤러의 탭 모음 항목을 설정해도 제목에 영향을주지 않습니다 navigationItem.title. tabBarItem탭 막대 컨트롤러에서 선택하려면 대신 탐색 컨트롤러에을 설정해야합니다 .

내 탭 바의 뷰 컨트롤러에는 모두 루트에 탐색 컨트롤러가 있기 때문에 다른 사람이 게시 한 답변 중 어느 것도 나를 위해 일하지 않았습니다. 이것은 UITabBarController. 당신은 네비게이션 컨트롤러의 설정해야 tabBarItem으로부터 다르게 보여 타이틀을 얻기 위해 대신 navigationItem의 제목

tabBarItem직접 생성하여 VC에 연결할 수 있습니다 .

    let tabBarVCOne = BooksListViewController()
    tabBarVCOne.tabBarItem = UITabBarItem(title: "Books", image: nil, tag: 0)

    tabBarViewControllers.append(tabBarVCOne)
    ...

그러면 다음과 같은 것이 생깁니다.

    //Wrap each view controller in a navigation controller. 
    self.viewControllers = tabBarViewControllers.map(UINavigationController.init)

그러나 tabBarItem뷰 컨트롤러에서 이미 연결된 항목을 가져와 탐색 컨트롤러에 자동으로 설정 하려면 다음과 같이 변경해야합니다 .

    self.viewControllers = tabBarViewControllers.map({
        let navigationController = UINavigationController(rootViewController: $0)
        navigationController.tabBarItem = $0.tabBarItem
        return navigationController
    })

이제 .NET에 정의 된 제목과는 별도로 VC에서 설정 한 다른 제목을 가질 수 있습니다 tabBarItem.


이것에 꽤 늦었습니다. TabBarController를 UITabBarControllerDelegate 및 UINavigationControllerDelegate로 사용하고 각 탭에 포함 된 탐색 컨트롤러로 사용할 수 있습니다.

.h :

@interface YourTabBarController : UITabBarController <UITabBarControllerDelegate, UINavigationControllerDelegate>

@end

.미디엄:

- (void) viewDidLoad {
    // UITabBarControllerDelegate
    self.delegate = self;

    // UINavigationControllerDelegates
    yourNavigationController.delegate = self;
    ...
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    yourNavigationController.tabBarItem.title = @"Tab Bar Title";
    ...
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    yourNavigationController.tabBarItem.title = @"Tab Bar Title";
    ...
}

Based on some quick testing, it seems like these two delegate actions should cover any loose cases and will update the title whether you're switching tabs or browsing in your navigation controller. For completeness, you could update your title in didShowViewController as well, but based on what I've seen, I don't think it's necessary.


Probably a bit late (but).

Setting the title of a VC changes the title of the Navigation AND the tabBar. (if the VC is already attached to both).

If you want to have separate titles, you need to manually set those, you normally set the title for the VC and then specifically the title of the tabBarItem, since it's a property of the


Swift 4.2

Here you go, I created an extension for UIViewController:

import UIKit

extension UIViewController {

/// Setting the navigation title and tab bar title
///
/// - Parameters:
///   - navigationTitle: Navigation title
///   - tabBarTitle: TabBar title
func setTitles(navigationTitle: String, tabBarTitle: String) {
    // Order is important here!
    title = tabBarTitle
    navigationItem.title = navigationTitle
 }

}

And then from your controller:

override func viewDidLoad() {
    super.viewDidLoad()
    setTitles(navigationTitle: "Login", tabBarTitle: "Home")
}

참고URL : https://stackoverflow.com/questions/21615637/how-to-change-uiviewcontroller-title-independent-of-tabbar-item-title

반응형