24

ナビゲーション バーに 2 つの rightBarButtonItems を配置したいと思います。1 つは編集用、もう 1 つは追加用です。

明らかに、Interface Builder を使用して作成することはできません。

プログラムでそれを行う方法を知っている人はいますか? ありがとう!

4

9 に答える 9

68

これは現在 iOS 5 に含まれており、rightBarButtonItems と呼ばれています。

Apple ドキュメントのテキストは次のとおりです。

rightBarButtonItems

レシーバーがトップ ナビゲーション アイテムの場合に、ナビゲーション バーの右側に表示するカスタム バー ボタン アイテムの配列。

@property(nonatomic, copy) NSArray *rightBarButtonItems

討論


この配列には、ナビゲーション バーの右側に表示する 0 個以上のバー ボタン項目を含めることができます。項目は、配列に表示される順序と同じ順序で右から左に表示されます。したがって、配列の最初の項目が一番右の項目になり、他の項目は前の項目の左側に追加されます。

配列内のすべてのアイテムを表示する十分なスペースがない場合、タイトル ビュー (存在する場合) またはバーの左側のボタンに重なるアイテムは
表示されません。

配列の最初の項目は、rightBarButtonItem プロパティを使用して設定することもできます。

UINavigationBar.h で宣言

ナビゲーション バーの右側に検索アイコンと編集アイコンを実装する方法を次に示します。

UIBarButtonItem *searchButton         = [[UIBarButtonItem alloc]
                                         initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                         target:self
                                         action:@selector(searchItem:)];

UIBarButtonItem *editButton          = [[UIBarButtonItem alloc] 
                                         initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                         target:self action:@selector(editItem:)];

self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:editButton, searchButton, nil];
于 2012-02-04T19:25:31.237 に答える
37

2 つのボタンを rightbarbutton として追加する方法の例を次に示します。以下のコードは、上矢印ボタンと下矢印ボタンを含むセグメント化されたコントロールを作成し、ナビゲーションの右バーボタンのカスタム ビューとして追加します。

UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
    [segmentedControl setMomentary:YES];
    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-up.png"] atIndex:0 animated:NO];
    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-down.png"] atIndex:1 animated:NO];
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
    self.navigationItem.rightBarButtonItem = segmentBarItem;
于 2009-09-01T13:38:53.910 に答える
4

私の提案は、追加機能をナビゲーション バーのボタンとして実装しないことです。以下のアイテムのテーブル ビューを扱っていると想定しているため、このユーザー インタラクションを処理する 1 つの方法は、テーブル ビューの最後のエントリとして [新しいアイテムを追加] オプションを表示することです。これは、ユーザーがナビゲーション バーの [編集] ボタンをタップしたときに、次のデリゲート メソッドを実装することにより、プログラムでフェードインできます。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView beginUpdates];
    [self.tableView setEditing:editing animated:YES];

    if (editing)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];           
    }
    else
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];     
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];           
    }
    [self.tableView endUpdates];
}

次に、以下を使用して行数を増やして、余分な行が考慮されるようにする必要があります。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if (tableView.editing)
        return ([objects count] + 1);
    else
        return [objects count];     
}

通常の削除編集スタイルとは対照的に、左側に緑色のプラス記号を表示します。

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
    if (indexPath.row >= [objects count]) 
        return UITableViewCellEditingStyleInsert;
    else
        return UITableViewCellEditingStyleDelete;

    return UITableViewCellEditingStyleNone;
}

もちろん、 cellForRowAtIndexPath: 実装で名前を指定し、その行選択も処理する必要があります。

于 2009-05-31T21:50:25.233 に答える
3

誰かが通りかかった場合、ここに迅速な答えがあります:

let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)
于 2016-07-18T16:00:38.737 に答える
2

ナビゲーション バーは UIView であるため、単純に規制 UIButton を作成し、サブビューとしてナビゲーション バーに追加できます。

ナビゲーション バーを基準にしてフレームを設定します。組み込みのボタンとまったく同じように見せたい場合は、SDK AFAIK に公開されていないため、グラフィックを自分で作成する必要があります。

于 2009-05-31T07:59:55.473 に答える
2

2 つの別個のボタンを使用したい場合は、右のバー項目に as custom ビューをrightBarButtonItem追加することで実現できます。UIToolbar

/*************************************************
       CREAT TWO RIGHT BAR BUTTON ITEMS
   *************************************************/
    // create a toolbar to have two buttons in the right
    UIToolbar* customToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];

    // create the array to hold the buttons, which then gets added to the toolbar
    NSMutableArray* rightBarButtonArray = [[NSMutableArray alloc] initWithCapacity:2];

    //Add the info button to the array
    UIButton* infoViewButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
    [infoViewButton addTarget:self action:@selector(showInfoView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *infoItem =  [[UIBarButtonItem alloc] initWithCustomView:infoViewButton];
    [rightBarButtonArray addObject:infoItem];
    [infoItem release];

    //Add the Done Button to the array
    UIBarButtonItem *bbi;
    bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                        target:self 
                                                        action:@selector(create:)];

    [rightBarButtonArray addObject:bbi];
    [bbi release];

    //add the array to the custom toolbar
    [customToolbar setItems:rightBarButtonArray animated:NO];
    [rightBarButtonArray release];

    // and finally add the custom toolbar as custom view to the right bar item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar];
    [customToolbar release];
于 2011-08-26T15:59:53.773 に答える
2

これが本当に簡単な2行の答えです:

ステップ 1: 必要なコンテンツを含むカスタム ビューのペン先を作成する

ステップ 2: ペン先をカスタム ビューとしてツールバーに追加します。

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TwoButtonView" owner:self options:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[subviewArray objectAtIndex:0]];
于 2011-09-04T08:00:08.017 に答える
0

(セグメント化されたコントロールボタンの代わりに)独立したボタンを表示するには:

// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];

// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];

// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];

// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];

// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
于 2012-02-27T08:44:31.183 に答える
0

Interface Builder から実行できます。アプリで行ったこと - ナビゲーション バーの左側の項目に UIView を追加し、このビューに 3 つのボタンを配置して、それらのアクションを接続しました。これに従って、左右のナビゲーション項目に複数のボタンを追加できます。

参考:サブニブのツールバー項目

ここに画像の説明を入力

于 2015-01-28T07:26:57.553 に答える