Cocoa touch 从UINavigationBar中消失的导航项

Cocoa touch 从UINavigationBar中消失的导航项,cocoa-touch,Cocoa Touch,我正在通过创建图像视图并执行[navigationController.navigationBar addSubview:imageView]将自定义背景图像添加到导航栏 背景很好。但一旦我这么做,我的其他导航项就会开始以一种奇怪的方式运行。有时出现navigationItem.title,返回按钮消失。这完全是随机的。你知道这是什么原因吗 谢谢。在视图设置过程中,您是在添加子视图吗?你可能是在你的其他导航项目被添加后才这样做的,所以它涵盖了它们。请尝试在-loadView方法中添加它,或者等待

我正在通过创建图像视图并执行
[navigationController.navigationBar addSubview:imageView]
将自定义背景图像添加到导航栏

背景很好。但一旦我这么做,我的其他导航项就会开始以一种奇怪的方式运行。有时出现
navigationItem.title
,返回按钮消失。这完全是随机的。你知道这是什么原因吗


谢谢。

在视图设置过程中,您是在添加子视图吗?你可能是在你的其他导航项目被添加后才这样做的,所以它涵盖了它们。请尝试在-loadView方法中添加它,或者等待-viewwillbeen:,并在添加后使用-sendSubviewToBack:。

在视图设置过程中,您何时添加子视图?你可能是在你的其他导航项目被添加后才这样做的,所以它涵盖了它们。尝试在-loadView方法中添加它,或者等到-viewwillbecome:,并在添加后使用-sendSubviewToBack:。

将imageView设置为索引0并解决了我的问题。

将imageView设置为索引0并解决了我的问题。

作为一个额外的想法,通过覆盖
drawRect:
使其显示背景图像,我成功地实现了一个完全定制的导航栏。从那里,可以轻松设置导航控制器的
导航项
属性的
左按钮项
右按钮项
标题视图
属性,以实现自定义按钮和标题。这与内置的导航控制器/导航栏/导航项逻辑配合使用,因此不会有任何东西(如栏按钮)被隐藏或模糊的风险

UINavigationBar
类别中:

- ( void )drawRect:( CGRect )rect
{
    [ [ UIImage imageNamed:@"background-image.png" ] drawInRect:CGRectMake( 0, 0, 
      self.frame.size.width, self.frame.size.height ) ];
}
在调用
pushViewController:animated:
之前,执行以下操作:

UIButton * leftButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
leftButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ leftButton setImage:[ UIImage imageNamed:@"left.png" ] forState:UIControlStateNormal ];
[ leftButton addTarget:self action:@selector( leftAction ) forControlEvents:UIControlEventTouchUpInside ];

UIButton * rightButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
rightButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ rightButton setImage:[ UIImage imageNamed:@"right.png" ] forState:UIControlStateNormal ];
[ rightButton addTarget:self action:@selector( rightAction ) forControlEvents:UIControlEventTouchUpInside ];

UIImageView * titleView = [ [ UIImageView alloc ] initWithImage:[ UIImage imageNamed:@"title.png" ] ];

UIBarButtonItem * leftBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView :leftButton ];
UIBarButtonItem * rightBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView:rightButton ];

nextController.navigationItem.leftBarButtonItem = leftButtonItem;
nextController.navigationItem.rightBarButtonItem = rightButtonItem;
nextController.navigationItem.titleView = titleView;
它可能有点忙,但您可以将所有这些代码抽象到其他地方的方法(或其系列)中。如果您想完全覆盖导航栏中出现的任何样式,这种方法往往效果良好


希望这有帮助

另一个想法是,我通过覆盖
drawRect:
使其显示背景图像,实现了一个完全定制的导航栏。从那里,可以轻松设置导航控制器的
导航项
属性的
左按钮项
右按钮项
标题视图
属性,以实现自定义按钮和标题。这与内置的导航控制器/导航栏/导航项逻辑配合使用,因此不会有任何东西(如栏按钮)被隐藏或模糊的风险

UINavigationBar
类别中:

- ( void )drawRect:( CGRect )rect
{
    [ [ UIImage imageNamed:@"background-image.png" ] drawInRect:CGRectMake( 0, 0, 
      self.frame.size.width, self.frame.size.height ) ];
}
在调用
pushViewController:animated:
之前,执行以下操作:

UIButton * leftButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
leftButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ leftButton setImage:[ UIImage imageNamed:@"left.png" ] forState:UIControlStateNormal ];
[ leftButton addTarget:self action:@selector( leftAction ) forControlEvents:UIControlEventTouchUpInside ];

UIButton * rightButton = [ UIButton buttonWithType:UIButtonTypeCustom ];
rightButton.frame = CGRectMake( 0.0, 0.0, 65.0, 32.0 );
[ rightButton setImage:[ UIImage imageNamed:@"right.png" ] forState:UIControlStateNormal ];
[ rightButton addTarget:self action:@selector( rightAction ) forControlEvents:UIControlEventTouchUpInside ];

UIImageView * titleView = [ [ UIImageView alloc ] initWithImage:[ UIImage imageNamed:@"title.png" ] ];

UIBarButtonItem * leftBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView :leftButton ];
UIBarButtonItem * rightBarButtonItem = [ [ UIBarButtonItem alloc ] initWithCustomView:rightButton ];

nextController.navigationItem.leftBarButtonItem = leftButtonItem;
nextController.navigationItem.rightBarButtonItem = rightButtonItem;
nextController.navigationItem.titleView = titleView;
它可能有点忙,但您可以将所有这些代码抽象到其他地方的方法(或其系列)中。如果您想完全覆盖导航栏中出现的任何样式,这种方法往往效果良好


希望这有帮助

如果您在ViewDid上设置它,它似乎工作得很好。谢谢;)如果您在ViewDid上设置它,它似乎工作得很好。谢谢;)