14

カスタムナビゲーションバーを作成したい。

ナビゲーションバーのタイトルの代わりにUIButtonを中央で使用できることは知っていますが、この画像のようなものを作成できますか?

ご覧のとおり、このナビゲーションバーの中央には3つの異なるボタンがあります。iPhoneでそのようなことをどのように実装できるかについて、あなたの考えを私と共有できますか?

4

2 に答える 2

17

ナビゲーションコントローラーを使用していると仮定すると、navigationBarのtitleViewプロパティを、中央に3つのボタン(および3つのドットの2つのセット)を保持する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. このように真ん中に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 に答える