iphone - ios : create NavigationBar programmatically, 但无法设置标题

标签 iphone ios navigation uinavigationbar

我在不使用 xib 文件的情况下以编程方式创建了一个 NavigationBar。 并创建一个带有文本“left”的左按钮,添加到导航栏,并将标题设置为“Demo”。

代码:

-(void) createNavBar:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{

    self.navBarController = [[[UINavigationController alloc] init] autorelease];
    navBar = [navBarController navigationBar];
    navBarController.navigationBar.bounds = CGRectMake(0, 0, 320, navBarHeight);
    [navBarController.navigationBar  setBackgroundImage:[[UIImage imageNamed:@"www/images/ios_hd_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 2, 0)]
     forBarMetrics:UIBarMetricsDefault];



    [navBarController view];

    navBarController.navigationItem.title = @"title";

    [navBarController.view setFrame:navBarController.navigationBar.bounds];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button = [self renderNavBarTextBtn:@"left" withButton:button direction:@"left"];
    [navBar addSubview:button];

    [[[self webView] superview] addSubview: navBarController.view];
}

但是程序可以成功设置navigationbar背景图,显示左键,但是不能设置title。这么奇怪?

补充: 我使用 cordova 框架来调用 .并且该接口(interface)仅提供给 cordova 调用。 感谢下面的回答,但我仍然无法解决我的问题。

屏幕截图:(注意:显示左键但不显示标题)

enter image description here

最佳答案

 UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = _backButton;

    self.navigationItem.title=@"Demo";
    [_backButton release], 
    [_backButton = nil];

关于iphone - ios : create NavigationBar programmatically, 但无法设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512288/

相关文章:

ios - 非 float UITableView 页脚

iphone - 为什么这个NSMutableSet不会泄漏内存

android - 导航回 fragment 时恢复回收器 View 状态 - 使用 jetpacks 导航架构

javascript - Jquery:当悬停在菜单项上时,显示文本

ios - 使用 UITapGestureRecognizer 从点击的单元格中获取标签文本

html - Bootstrap 中的水平下拉菜单不会随导航滚动

iphone - MKMapView 当前位置显示为自定义图钉

iphone - 如何根据日期对sqlite数据库进行排序?

ios - 使用推送 View Controller 时如何将 Uiimage 从一个 View Controller 传递到其他 View Controller ?

multithreading - CLLocationmanager从后台线程更新