繁体   English   中英

ios:以编程方式创建NavigationBar,但无法设置标题

[英]ios : create NavigationBar programmatically, but cannot set the title

我不使用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];
}

但是,该程序可以成功设置导航栏背景图像,显示左按钮,但不能设置标题。 这么奇怪?

补充:我使用cordova框架调用。 并且仅将接口提供给cordova调用。 感谢您提供以下答案,但我仍然无法解决我的问题。

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

在此处输入图片说明

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

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

请尝试以下方法:

self.navigationItem.title=@"Your Title";

navBar.title =@"Your title";

您可以设置视图标题,该标题会显示在导航栏上

self.title=@"My Navigation View";  

或者,您可以在要显示的视图中设置navigationBarTitle

self.navigationItem.title=@"My Navigation";

试试这个代码片段:

//nativeControls.hideLeftNavButton();

    nativeControls.setupRightNavButton(
        "About",
        "",
        "onRightNavButton"
    );

    nativeControls.showNavBar();

否则请参考此博客:http: //zsprawl.com/iOS/2012/05/navigation-bar-with-nativecontrols-in-cordova/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM