14

我想创建一个自定义导航栏。

我知道我们可以在中间使用 UIButton 来代替导航栏中的标题,但是我们可以制作一些看起来像这张图片的东西吗?

如您所见,我们在此导航栏的中间有三个不同的按钮。您能否与我分享您的想法,我们如何在 iPhone 中实现这样的功能?

4

2 回答 2

17

假设您使用的是导航控制器,您可以将导航栏的 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 中完成这一切。

于 2012-09-19T20:42:19.020 回答
4

如果您使用的是 UINavigationController,请尝试此操作

  1. 更改导航栏的背景图像[self.navigationController.navigationBar setBackgroundImageForBarMetrics:]
  2. [self.navigationItem setLeftBarButtonItem:]用和设置您的左右按钮[self.navigationItem setRightBarButtonItem:]。您可能必须在此处使用带有 UIButton 的 UIBarButtonItem 作为自定义视图来摆脱边框。
  3. 像这样设置中间的三个按钮

(您可能必须更改尺寸)

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;
于 2012-09-19T20:50:16.650 回答