web-dev-qa-db-ja.com

UITableViewControllerにツールバーを追加

UIToolBarをUITableViewControllerに追加する最も簡単な方法は何ですか?私は編集機能に依存しているため、UITableViewControllerをUIViewControllerに簡単に変更できません。

37
Farcaller

まったく問題ありません。UITableViewControllerUIViewControllerのサブクラスです。そして、iPhone OS 3.0では、UIViewController(およびサブクラス)UINavigationControllerと連動してコンテキスト認識ツールバーを提供することができます。

これが機能するためには、次のことを行う必要があります。

  • UINavigationControllerを使用して、ツールバーを必要とするすべてのView Controllerが含まれていることを確認してください。
  • ツールバーを必要とするビューコントローラーのtoolbarsItemsプロパティを設定します。

これは、ビューコントローラのタイトルを設定するのとほとんど同じくらい簡単で、同じ方法で行う必要があります。ほとんどの場合、initWithNibName:bundle:イニシャライザをオーバーライドします。例として:

-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
  self = [super initWithNibName:name bundle:bundle];
  if (self) {
    self.title = @"My Title";
    NSArray* toolbarItems = [NSArray arrayWithObjects:
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                      target:self
                                                      action:@selector(addStuff:)],
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                                                      target:self
                                                      action:@selector(searchStuff:)],
        nil];
    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;
    self.navigationController.toolbarHidden = NO;
  }
  return self;
}

toolbarItemsプロパティに割り当てる代わりにsetToolbarItems:animated:を使用して、ツールバーの項目をその場でアニメーション形式で追加および削除することもできます。

51
PeyloW

PeyloWのレシピを機能させるために、次のコード行を追加する必要がありました。

self.navigationController.toolbarHidden = NO;

お役に立てば幸い...

40
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    //Initialize the toolbar
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];

    //Caclulate the height of the toolbar
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view.
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver
    [toolbar setFrame:rectArea];

    //Create a button
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];



[[self tableView] reloadData];

}

- (void) info_clicked:(id)sender {


 [self.navigationController popViewControllerAnimated:YES];
    [toolbar removeFromSuperview];

    }

そしてSwift 3:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //Initialize the toolbar
    let toolbar = UIToolbar()
    toolbar.barStyle = UIBarStyle.default

    //Set the toolbar to fit the width of the app.
    toolbar.sizeToFit()

    //Caclulate the height of the toolbar
    let toolbarHeight = toolbar.frame.size.height

    //Get the bounds of the parent view
    let rootViewBounds = self.parent?.view.bounds

    //Get the height of the parent view.
    let rootViewHeight = rootViewBounds?.height

    //Get the width of the parent view,
    let rootViewWidth = rootViewBounds?.width

    //Create a rectangle for the toolbar
    let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)

    //Reposition and resize the receiver
    toolbar.frame = rectArea

    //Create a button
    let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))

    toolbar.items = [infoButton]

    //Add the toolbar as a subview to the navigation controller.
    self.navigationController?.view.addSubview(toolbar)
}

func infoClicked() {
    //Handle Click Here
}
14
user283846