我想创建一个acustom导航栏。
我知道我们可以在导航栏中间使用UIButton而不是标题,但是我们可以制作看起来像这张照片的东西吗?
如您所见,此导航栏中间有三个不同的按钮。你能跟我分享一下我们如何在iPhone上实现这样的事情吗?
答案 0 :(得分:17)
假设您使用的是导航控制器,您可以将navigationBar的titleView属性设置为UIView容器,该容器包含中间的三个按钮(以及两组三个点)。代码看起来像这样:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame:CGRectMake(0, 0, 44, 44)];
[button0 setBackgroundImage:[UIImage imageNamed:@"button0.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(button0Action:) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = buttonContainer;
或者你当然可以在IB中完成这一切。
答案 1 :(得分:3)
如果您正在使用UINavigationController
,请尝试此操作[self.navigationController.navigationBar setBackgroundImageForBarMetrics:]
[self.navigationItem setLeftBarButtonItem:]
和[self.navigationItem setRightBarButtonItem:]
设置左右按钮。您可能必须使用UIBarButtonItem和UIButton作为自定义视图来摆脱边界。(您可能需要更改尺寸)
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 40)];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.imageView.image = [UIImage imageNamed:@"button1.png"];
button1.frame = CGRectMake(0, 0, 40, 40);
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.imageView.image = [UIImage imageNamed:@"button2.png"];
button2.frame = CGRectMake(70, 0, 40, 40);
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.imageView.image = [UIImage imageNamed:@"button3.png"];
button3.frame = CGRectMake(140, 0, 40, 40);
[buttonView addSubview:button1];
[buttonView addSubview:button2];
[buttonView addSubview:button3];
self.navigationItem.titleView = buttonView;