navigationBar中间的按钮
我想创建一个acustom导航栏。
我知道我们可以在导航栏中间使用UIButton而不是标题,但是我们可以制作看起来像这张照片的东西吗?
如您所见,我们在此导航栏的中间有三个不同的按钮。 你能和我分享你的想法,我们如何在iPhone中实现这样的东西?
假设您正在使用导航控制器,您可以将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中完成这一切。
如果您正在使用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;