0

特定の位置にあるナビゲーションバーに、カスタム画像付きの2つのボタンを追加したいと思います。

私は解決策を見つけましたが、それは右/左のナビゲーションバーボタン用です。

そのための私のコードは次のとおりです。

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];

// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];

これどうやってするの?

4

4 に答える 4

4
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];

[self.navigationController.navigationBar addSubview:vieww];    

また、yourButtonViewを削除する場合、makeはグローバルオブジェクトです。

in .h

UIView *vieww;

とで.m

-(void)viewWillDisappear:(BOOL)animated
{
    [vieww removeFromSuperview];
}

または、これに従って リンクを増やしてください

于 2012-12-15T07:47:34.100 に答える
1

> iOS 5を使用している場合は、これを使用してください。

UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)];
    UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];

<iOS 5の場合、uは次を使用できます。

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
        tools.barStyle = UIBarStyleBlackOpaque;
        // create the array to hold the buttons, which then gets added to the toolbar
        NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
        [buttons addObject:btn1];
        [buttons addObject:btn2];
        [tools setItems:buttons animated:NO];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
于 2012-12-15T09:10:06.250 に答える
0

ツールバーを追加する代わりに、1つのUIViewを作成し、そのビューに2つのボタンを追加できます。

    UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
于 2012-12-15T07:47:24.973 に答える
0

ストーリーボードの使用と組み合わせてこれを実行したい場合は、この質問を参照してください。

于 2013-06-10T11:43:32.570 に答える