2

私はiPhone開発者に不慣れです、

ナビゲーションバーの[戻る]ボタンの横にもう1つボタンを追加したいのですが、ナビゲーションコントローラーを使用しているので、[戻る]ボタンは既に左側にあります。右端にボタンを1つ追加しました

self.navigationItem.rightBarButtonItem = ModeButton;

しかし、私が書くとき、

self.navigationItem.leftBarButtonItem = NewButton;

NewButtonは戻るボタンをオーバーライドします。新しいボタンを戻るボタンの横に配置したいと思います。

どんな助けでもありがたいです。

4

4 に答える 4

4

UINavigationItemにはプロパティがありleftItemsSupplementBackButton、それを使用できます。

leftItemsSupplementBackButton 戻るボタンに加えて左のアイテムが表示されるかどうかを示すブール値。

@property BOOL leftItemsSupplementBackButton

ディスカッション 通常、カスタムの左バー ボタン アイテムが存在すると、カスタム アイテムを優先して戻るボタンが削除されます。このプロパティを YES に設定すると、 leftBarButtonItems プロパティまたは leftBarButtonItem プロパティの項目が戻るボタンの右側に表示されます。つまり、戻るボタンの代わりではなく、追加して表示されます。NO に設定すると、戻るボタンの代わりにこれらのプロパティの項目が表示されます。このプロパティのデフォルト値は NO です。

UINavigationController Class Referenceから。

UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
于 2012-07-04T09:06:43.697 に答える
1

他のスペースと重ならないように、柔軟なスペースが必要だと思います。おそらく、このようなものから始める必要があります。

    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back:)];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"newButton" style:UIBarButtonItemStyleBordered target:self action:@selector(newButtonMethod:)];

    NSArray *navBarItems = [[NSArray alloc] initWithObjects:back, flexibleSpace, newButton, nil];

編集:

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];  
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back:)];
[buttons addObject:back];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttons addObject:flexibleSpace];
    UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"newButton" style:UIBarButtonItemStyleBordered target:self action:@selector(newButtonMethod:)];
[buttons addObject:newButton];

UIToolbar* myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 45)];
        [myToolbar setTintColor:[self.navigationController.navigationBar tintColor]];
 [myToolbar setItems:buttons animated:NO];

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithCustomView:myToolbar];

self.navigationItem.leftBarButtonItem = myButton;
于 2012-07-04T09:08:44.273 に答える
0

これを実現するには、カスタムを作成する必要がありますUIBarButtonItem。次のように、BackButton(ユーザー定義)とNewButtonの両方のボタンをそれぞれ配置する必要があります。

UIView *viewForLeftBarButton = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 100, 35)];
[viewForLeftBarButton addSubview:BackButton];
[viewForLeftBarButton addSubview:NewButton];

UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:viewForLeftBarButton];

self.navigationItem.leftBarButtonItem = leftBarButtonItem;
于 2012-07-04T09:11:26.553 に答える
0

2 つのボタンを含むカスタム ビューを 1 つ作成し、次のようにバー ボタン アイテムを作成します。

UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:userCustomView];

self.navigationItem.leftBarButtonItem = leftBarButtonItem;

編集: 以下のコードは、2 つのボタンを作成する方法の例にすぎません。

UIView *customView = [[UIView alloc] init];
customView.frame = CGRectMake(0, 0, 100, 40);

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"Add" forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 50, 40);

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"del" forState:UIControlStateNormal];
button2.frame = CGRectMake(60, 0, 50, 40);
[customView  addSubview:button2];
[customView addSubview:button1];

UIBarButtonItem *barItem =[[UIBarButtonItem alloc] initWithCustomView:customView];


self.navigationItem.leftBarButtonItem = barItem;
于 2012-07-04T09:04:13.497 に答える