カスタムナビゲーションバーを作成したい。
ナビゲーションバーのタイトルの代わりにUIButtonを中央で使用できることは知っていますが、この画像のようなものを作成できますか?
ご覧のとおり、このナビゲーションバーの中央には3つの異なるボタンがあります。iPhoneでそのようなことをどのように実装できるかについて、あなたの考えを私と共有できますか?
カスタムナビゲーションバーを作成したい。
ナビゲーションバーのタイトルの代わりにUIButtonを中央で使用できることは知っていますが、この画像のようなものを作成できますか?
ご覧のとおり、このナビゲーションバーの中央には3つの異なるボタンがあります。iPhoneでそのようなことをどのように実装できるかについて、あなたの考えを私と共有できますか?
ナビゲーションコントローラーを使用していると仮定すると、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で行うこともできます。
UINavigationControllerを使用している場合は、これを試してください
[self.navigationController.navigationBar setBackgroundImageForBarMetrics:]
[self.navigationItem setLeftBarButtonItem:]
ます[self.navigationItem setRightBarButtonItem:]
。境界線を取り除くには、ここでカスタムビューとしてUIButtonとともにUIBarButtonItemを使用する必要があります。(おそらく寸法を変更する必要があります)
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;