设置UINavigationBar的标题

chi*_*kad 6 objective-c title uinavigationbar uikit ios

self.navigationItem.title = @"MyTitle";视图加载后是否可以设置?

我正在使用搜索栏,在同一视图中,我使用搜索结果提供UITableView.所以我想在搜索结果加载后将搜索栏的文本值设置为导航栏的标题.

我使用的是iOS 4.3.

Sam*_*hen 15

只需设置如下标题UINavigationBar:

navBar.topItem.title = @"sample string";
Run Code Online (Sandbox Code Playgroud)


chi*_*kad 2

找到了解决方案。我关注了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]; 
Run Code Online (Sandbox Code Playgroud)