iphone 4.3导航栏问题

发布于 2025-01-08 17:08:19 字数 2185 浏览 3 评论 0原文

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

}

I am now developing an app which support both iOS 4.3 and 5.0.

My code works fine with 5.0, but results in a tricky-bug in 4.3.

Issue is:

I've got a view with a tableView in it. This view have a navigation bar with both left and right navigation bar items. Once I select the row in table view (and reaches it's corresponding view) and returns back, both the left and right navigation-bar items "vanish".

I've been over this mess for last few hours, some one got a cure?

This is what I have already done:

This is the utility method that I call when view loads.

+ (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];


}

Have tried calling this both in viewDidLoad and viewWillAppear, but in vain.

This is how I call this in viewController.

- (void)viewDidLoad
{

[super viewDidLoad];

//other setups here.

[Utility setNavigationBarContents:self]; 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2025-01-15 17:08:20

我通过添加得到了解决方案
我的 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

I got the solution by adding
This code in my 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文