2

我现在正在开发一个同时支持 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]; 

}
4

1 回答 1

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
于 2012-02-23T03:08:30.677 回答