從iOS 5開始,UINavigationController可透過setTitleTextAttributes自訂navigationBar標題樣式。
例如:
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0 green:0.7 blue:0.8 alpha:1], UITextAttributeTextColor, [UIColor colorWithRed:0 green:0.7 blue:0.8 alpha:1], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Arial-Bold" size:0.0], UITextAttributeFont, nil]];其中UITextAttributeTextColor控制文字顏色,UITextAttributeFont控制字體,UITextAttributeTextShadowColor與UITextAttributeTextShadowOffset則可調整陰影效果。
實務上若要讓標題更清晰,可將字體設為系統字型並避免使用0大小,以免造成顯示異常。若App有多個導航列風格,也可在進入頁面前動態更新,以維持視覺一致性。