导航栏按钮不显示(Navigation bar button not showing)

2019-10-17 16:39发布

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

这就是我在的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;

请帮忙。

Answer 1:

我认为没有错与您的代码只是检查你的图像或带有圆角的矩形图像尝试测试

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;



Answer 2:

问题出在下面一行:

self.navigationController.navigationItem.leftBarButtonItem=random1;

只需使用

self.navigationItem.leftBarButtonItem=random1;


Answer 3:

希望它会为你工作?

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


Answer 4:

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.Hope它为周到,谢谢同样的方法



Answer 5:

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(nextToPhoneAuth)];
self.navigationItem.backBarButtonItem = barBtnItem


Answer 6:

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



文章来源: Navigation bar button not showing