我想這個說法來改變導航欄標題:是否可以在viewDidLoad/viewWillAppear之外更改導航欄標題?
[[self navigationItem] setTitle:myTitle];
它工作正常,在viewDidLoad中和viewWillAppear中,但它不工作,其他地方。我正在嘗試做什麼?這是我正在使用的視圖控制器。
我想這個說法來改變導航欄標題:是否可以在viewDidLoad/viewWillAppear之外更改導航欄標題?
[[self navigationItem] setTitle:myTitle];
它工作正常,在viewDidLoad中和viewWillAppear中,但它不工作,其他地方。我正在嘗試做什麼?這是我正在使用的視圖控制器。
有一個很簡單的方法來改變標題當前視圖控制器。
self.title = @"New Title";
的視圖控制器,其在導航控制器將導致在導航條與所述新的更新的標題所示的標題上調用此。
這可以在視圖控制器顯示的任何時候調用。
有沒有必要深入到視圖控制器的navigationItem
設置標題。
@ reid55你可以設置導航標題下面的方法也:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[self navigationItem] setTitle:myTitle];
}
return self;
}
它實際上是navigationItem將使用的視圖控制器的title屬性。上面的答案是在導航欄中更改標題的最簡單正確的方法。
如果您需要自定義您的導航欄,你做的方式,這是覆蓋在你的UIViewController子類,而不是改變它在viewDidAppear:或任何其他viewWill/DID方法
- (UINavigationItem*)navigationItem
{
UINavigationItem *navigationItem = [super navigationItem];
// customize it further than the default
return navigationItem;
}
此外,您可以對標題進行更改,使用KVO更新自定義標題視圖。您可以偵聽UIViewController的title屬性中的更改並更新navigationItem的titleView屬性。檢查了這一點:
static void *kObjectStateObservingContext = &kObjectStateObservingContext; // outside your @implementation bit
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// other init stuff here...
[self addObserver: self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context: kObjectStateObservingContext];
}
return self;
}
不要忘記刪除觀察者:
- (void)dealloc
{
[self removeObserver: self forKeyPath:@"title"];
}
而現在我們再次重溫navigationItem,在那裏你可以創建一個自定義的UILabel將更新,每當我們改變標題,之後,我們必須提供當標題不改變處理器實現:
- (UINavigationItem*)navigationItem
{
UINavigationItem *navigationItem = [super navigationItem];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(roundf((320 - 230)/2), 0, 230, 44)];
titleLabel.font = [UIFont systemFontOfSize: 18];
titleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"topnav-background-text-gradient.png"]]; // you can even draw the font with a gradient!
titleLabel.shadowColor = [UIColor colorWithWhite: 1.0 alpha:0.59];
titleLabel.shadowOffset = CGSizeMake(0, 1.0);
titleLabel.text = self.title;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
navigationItem.titleView = titleLabel;
return navigationItem;
}
最後,做什麼的時候self.title實際改變:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == kObjectStateObservingContext) {
// here we update the text of the titleView
if ([self.navigationItem.titleView isKindOfClass:[UILabel class]]) {
[(UILabel*)self.navigationItem.titleView setText: self.title];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
然後你走了!很明顯,你可能會想到更多的代碼,所以如果你現在不想,我建議任何人構建iOS應用程序,你總是爲你的應用程序創建一個基類視圖控制器(MyBaseViewController:UIViewController),然後爲其他子類創建子類視圖控制器在你的應用程序
從iOS 5.0起,您可以簡單地使用UINavigationBar的外觀API。不需要你發佈的任何代碼(截至iOS 5.0)。 – rmaddy
你可以發佈如何完成外觀的API嗎? – horseshoe7
由於您唯一改變的是'textColor',請使用'UINavigationBar titleTextAttributes'方法。如果您希望對應用中的每個導航欄進行更改,請調用'[UINavigationBar appearance] .titleTextAttributes = ...'。 – rmaddy
我知道這是舊的,但我想爲未來的用戶回答這個問題。我認爲大多數答案沒有涉及的主要問題是從主線程設置標題非常重要,否則有一些情況不會立即設置。對於Swift 3.0,您可以使用如下代碼:
DispatchQueue.main.async {
self.title = "Example string"
}
從主線程執行所有UI更新很重要。我不確定原始用戶是否通過異步閉包來設置標題,但這應該有助於任何遇到困難的人。
我試過這個,但它並沒有改變標題,即使視圖控制器正在顯示。當你說「在導航控制器中查看控制器」時,我不認爲我知道你的意思。請解釋。 – reid55
你是否將你的視圖控制器添加到'UINavigationController'中,或者你是否簡單地向視圖控制器添加了一個'UINavigationBar'? – rmaddy
我假設我將我的視圖控制器添加到導航控制器,因爲這是我從另一個視圖控制器調用它:[[self navigationController] pushViewController:sentenceViewController animated:YES]; – reid55