[ios] iPhone 탐색 막대 제목 텍스트 색상

iOS 탐색 막대 제목 색상이 기본적으로 흰색 인 것 같습니다. 다른 색으로 바꾸는 방법이 있습니까?

navigationItem.titleView이미지를 사용한 접근 방식을 알고 있습니다. 내 디자인 기술이 제한적이고 표준 광택을 얻지 못했기 때문에 텍스트 색상을 변경하는 것을 선호합니다.

모든 통찰력은 대단히 감사하겠습니다.



답변

현대적인 접근

내비게이션 컨트롤러의 루트보기가로드 될 때 전체 내비게이션 컨트롤러에 대한 현대적인 방법입니다.

[self.navigationController.navigationBar setTitleTextAttributes:
   @{NSForegroundColorAttributeName:[UIColor yellowColor]}];

그러나 이것은 후속보기에는 영향을 미치지 않습니다.

고전적인 접근

보기 컨트롤러 당 이전 방식 (이 상수는 iOS 6 용이지만 iOS 7 모양에서보기 컨트롤러별로 수행하려는 경우 동일한 접근 방법을 원하지만 다른 상수를 사용합니다) :

당신은을 사용할 필요 UILabel는 AS titleViewnavigationItem.

라벨은 :

  • 배경색이 깨끗해야합니다 ( label.backgroundColor = [UIColor clearColor]).
  • 굵은 체 20pt 시스템 글꼴 ( label.font = [UIFont boldSystemFontOfSize: 20.0f])을 사용하십시오 .
  • 50 % 알파 ( label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]) 의 검은 그림자가 있습니다.
  • 당신은 중심뿐만 아니라 (에 텍스트 정렬을 설정할 수 있습니다 label.textAlignment = NSTextAlignmentCenter( UITextAlignmentCenter이전의 SDK)입니다.

라벨 텍스트 색상을 원하는 색상으로 설정하십시오. 텍스트가 그림자로 혼합되지 않는 색상을 원하므로 읽기가 어렵습니다.

나는 시행 착오를 통해이 문제를 해결했지만, 내가 생각해 낸 값은 궁극적으로 Apple이 선택한 것이 아닌 너무 간단합니다. 🙂

당신이 확인하고 싶은 경우,이 코드를 드롭 initWithNibName:bundle:PageThreeViewController.m의 애플 Navbar의 샘플 . 텍스트가 노란색 레이블로 바뀝니다. 색상을 제외하고 Apple 코드에서 생성 한 원본과 구별 할 수없는 것이어야합니다.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = NSTextAlignmentCenter;
                           // ^-Use UITextAlignmentCenter for older SDKs.
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;
}

편집 : 또한 아래의 Erik B의 답변을 읽으십시오. 내 코드는 효과를 보여 주지만 그의 코드는 기존 뷰 컨트롤러에서이를 간단하게 배치 할 수있는 방법을 제공합니다.


답변

나는 이것이 꽤 오래된 스레드라는 것을 알고 있지만 iOS 5가 타이틀 속성을 설정하기 위해 새로운 속성을 제공한다는 것을 새로운 사용자에게 아는 것이 유용 할 것이라고 생각합니다.

UINavigationBar setTitleTextAttributes를 사용하여 글꼴, 색상, 오프셋 및 그림자 색상을 설정할 수 있습니다 .

또한 UINavigationBars응용 프로그램 전체 에서 동일한 기본 UINavigationBar의 제목 텍스트 속성을 설정할 수 있습니다 .

예를 들면 다음과 같습니다.

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor,
                                            [UIColor blackColor], UITextAttributeTextShadowColor,
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];


답변

iOS 5에서는 다음과 같은 방법으로 navigationBar 제목 색상을 변경할 수 있습니다.

navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};


답변

Steven Fisher의 답변을 바탕으로 다음 코드를 작성했습니다.

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color

        self.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = title;
    [titleView sizeToFit];
}

이 코드의 장점은 프레임을 올바르게 처리하는 것 외에도 컨트롤러의 제목을 변경하면 사용자 지정 제목보기도 업데이트된다는 것입니다. 수동으로 업데이트 할 필요가 없습니다.

또 다른 큰 장점은 커스텀 타이틀 색상을 활성화하는 것이 정말 간단하다는 것입니다. 이 방법을 컨트롤러에 추가하기 만하면됩니다.


답변

iOS 7에서 위의 제안 중 대부분은 더 이상 사용되지 않습니다.

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor whiteColor],NSForegroundColorAttributeName,
                               [UIColor whiteColor],NSBackgroundColorAttributeName,nil];

self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";

또한 설정할 수있는 다양한 텍스트 속성에 대해 NSAttributedString.h를 확인하십시오.


답변

iOS 7 및 8에서는 제목의 색상을 녹색으로 변경하도록 할 수 있습니다

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];


답변

질문을 최신 상태로 유지하기 위해 Alex RR 솔루션을 추가 하지만 Swift 에서는 추가하겠습니다 .

self.navigationController.navigationBar.barTintColor = .blueColor()
self.navigationController.navigationBar.tintColor = .whiteColor()
self.navigationController.navigationBar.titleTextAttributes = [
    NSForegroundColorAttributeName : UIColor.whiteColor()
]

결과는 다음과 같습니다.

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