1

当尝试在左侧和右侧添加导航按钮时,它没有显示?这是我使用的代码,这有什么问题?

这就是我在 appdelegate 中添加导航栏的方式,

  UIViewController *viewController1 = [[logyFirstViewController alloc] initWithNibName:@"logyFirstViewController" bundle:nil];
  UIViewController *viewController2 = [[logySecondViewController alloc] initWithNibName:@"logySecondViewController" bundle:nil];
  UIViewController *viewController3 = [[record alloc] initWithNibName:@"record" bundle:nil];
  UIViewController *viewController4 = [[searchview alloc] initWithNibName:@"searchview" bundle:nil];
  UIViewController *viewController5 = [[profilde alloc] initWithNibName:@"profilde" bundle:nil];
 self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4, viewController5];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];

self.navigationController.navigationBar.barStyle=UIBarStyleDefault;

[navigationController.navigationBar setAlpha:0.8f];
 [self.window addSubview:[navigationController view]];





 button4 = [UIButton buttonWithType:UIButtonTypeCustom];
 [button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
 [button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
 [button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
 UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];
 self.navigationController.navigationItem.leftBarButtonItem=random1;

请帮忙。

4

6 回答 6

13

我认为您的代码没有任何问题,只需检查您的图像或尝试使用圆形矩形图像进行测试

UIButton* button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];

并使用 self.navigationController.navigationBar.topItem.leftBarButtonItem=random1; 而不是 self.navigationController.navigationItem.leftBarButtonItem=random1;

于 2013-02-05T15:16:38.687 回答
3

问题出在以下行:

self.navigationController.navigationItem.leftBarButtonItem=random1;

只需使用

self.navigationItem.leftBarButtonItem=random1;
于 2013-02-05T12:45:20.910 回答
0
UIButton *button4 =  [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)]];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button4] autorelease];

对 leftBarButtonItem 使用相同的方法。希望它对你有用。谢谢

于 2013-02-05T12:38:32.007 回答
0

希望它对你有用...

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];
[button4 release];

[[self navigationItem] setLeftBarButtonItem:random1];
[random1 release];
于 2013-02-05T12:48:04.673 回答
0
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(nextToPhoneAuth)];
self.navigationItem.backBarButtonItem = barBtnItem
于 2013-02-05T12:56:07.343 回答
0

请检查您的导航栏是否提供了隐藏属性,并尝试使用默认的后退按钮一次,让我们知道它是否工作。

于 2013-02-05T12:58:33.827 回答