我正在开发一个支持iOS 4.3和5.0的应用程序。
我的代码在5.0下工作正常,但在4.3中会导致棘手的错误。
问题是:
我有一个带有tableView的视图。此视图的导航栏包含左侧和右侧导航栏项。一旦我在表格视图中选择了行(并到达它的相应视图)并返回,左右导航栏项目都“消失”。
过去几个小时我一直在这个烂摊子,有人得到了治疗吗?
这就是我已经做过的事情:
这是我在视图加载时调用的实用方法。
+ (void)setNavigationBarContents:(UIViewController *)view
{
UIImage *topBarimage=[UIImage imageNamed:NAVIGATION_BAR_IMAGE];
if([view.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[view.navigationController.navigationBar setBackgroundImage:topBarimage forBarMetrics:UIBarMetricsDefault];
}
else
{
[view.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:topBarimage] aboveSubview:view.navigationController.navigationBar];
}
UIImage *logo=[UIImage imageNamed:LOGO];
UIImageView *logoView=[[UIImageView alloc]initWithImage:logo];
view.navigationItem.titleView = logoView;
UIImage *settingsButtonImage= [UIImage imageNamed:NAVIGATION_SETTINGS];
UIButton *rightBarButton = [UIButton buttonWithType: UIButtonTypeCustom];
[rightBarButton setBackgroundImage: settingsButtonImage forState:UIControlStateNormal];
[rightBarButton addTarget: view action:@selector(settingsButton:) forControlEvents:UIControlEventTouchUpInside];
rightBarButton.frame = CGRectMake(0, 0, 50, 40);
view.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: rightBarButton];
UIImage *logoutButtonImage= [UIImage imageNamed:LOGOUT];
UIButton *leftBarButton = [UIButton buttonWithType: UIButtonTypeCustom];
[leftBarButton setBackgroundImage: logoutButtonImage forState:UIControlStateNormal];
[leftBarButton addTarget: view action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
leftBarButton.frame = CGRectMake(0, 0, 65, 32);
view.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: leftBarButton];
}
尝试在viewDidLoad和viewWillAppear中调用它,但是徒劳无功。
这是我在viewController中调用它的方式。
- (void)viewDidLoad
{
[super viewDidLoad];
//other setups here.
[Utility setNavigationBarContents:self];
}
答案 0 :(得分:3)
我通过添加解决方案 这个代码在我的appDelegate.m
中// added so that navigation bar background image works in version lower than 5.0
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:NAVIGATION_BAR_IMAGE];
[img drawInRect:rect];
}
@end