在视图加载后设置self.navigationItem.title = @"MyTitle";
是否可行?
我正在使用搜索栏,同时在同一视图中使用搜索结果填充UITableView。因此,我想在搜索结果加载后将搜索栏的文本值设置为导航栏的标题。
我正在使用iOS 4.3。
在视图加载后设置self.navigationItem.title = @"MyTitle";
是否可行?
我正在使用搜索栏,同时在同一视图中使用搜索结果填充UITableView。因此,我想在搜索结果加载后将搜索栏的文本值设置为导航栏的标题。
我正在使用iOS 4.3。
只需像这样设置UINavigationBar
的标题:
navBar.topItem.title = @"sample string";
我找到了一个解决方案。我按照Pedro Valentini的帖子所说进行操作。
这是我所做的:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = UITextAlignmentCenter;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
navBar.topItem.title
设置为更直接,并且将利用已经设置好的设计。 - Jay Whitsitt通常,titleView 将是一个 UILabel,因此您可以获取它、转换它并设置其文本。
由于某些原因,我在 viewDidLoad 中使用它时遇到了问题,在将其添加到 viewWillAppear 中似乎行得通。
((UILabel *)self.navigationController.navigationBar.topItem.titleView).text = @"your title here"
可以。您可以在UIViewController的任何生命周期中动态设置navigationItem的标题。设置操作由getter方法处理,该方法向UINavigationController发送消息以上传导航栏的视图内容以反映新标题。
如果self在“inside”一个UINavigationController
中,那应该可以工作。
_navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
_navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = @"Title";
试试这个。
self.navigationItem.title = @"Terms and Conditions";
self.title = @"MyTitle";
- Inder Kumar Rathore