Répondre

49

Aucun problème, UITableViewController est une sous-classe de UIViewController. Et il se trouve que dans iPhone OS 3.0 UIViewController(et sous-classes) peut fonctionner en conjonction avec un UINavigationController pour fournir une barre d'outils contextuelle.

Pour que cela fonctionne, vous devez:

  • Assurez-vous que vous utilisez un UINavigationController pour contenir tous vos contrôleurs de vue qui a besoin d'une barre d'outils.
  • Définissez la propriété toolbarsItems du contrôleur de vue qui souhaite une barre d'outils.

Ceci est presque aussi simple que de définir le titre du contrôleur de vue, et devrait être fait de la même manière. Très probablement en remplaçant l'initialiseur initWithNibName:bundle:. A titre d'exemple:

-(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; 
} 

Vous pouvez également utiliser setToolbarItems:animated: au lieu d'attribuer à la propriété toolbarItems, ajouter et supprimer des éléments de la barre d'outils d'une façon animée à la volée.

+0

Le contrôleur de navigation est-il requis? Je veux ajouter un ToolBar à un TableViewController qui ne fait pas partie d'un NavigationController. Ai-je besoin d'utiliser un NavigationController même s'il n'y aura qu'une seule vue? –

+0

@sirjorj Oui, 'UINavigationController' est requis pour obtenir la gestion de la barre d'outils * free *. Sans cela, vous devez gérer votre propre instance de vue 'UIToolbar'. – PeyloW

+0

Et si je ne veux pas mettre de boutons dans cette barre d'outils, à la place, je veux mettre seulement une image au centre, que ferais-je différemment? Merci. –

39

Afin de rendre la recette de PeyloW à travailler, je devais ajouter la ligne de code supplémentaire suivant:

self.navigationController.toolbarHidden = NO; 

espoir qui aide ...

+2

D'accord. J'ai dû mettre cet appel dans la méthode viewDidLoad, pas le remplacement initWithNibName. Ensuite, cela fonctionne très bien. –

+0

vous venez d'enregistrer ma journée, merci –

12
- (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]; 

    } 

Et 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 
} 
+0

Cela fonctionne très bien pour moi. Je ne pouvais pas ajouter de 'UINavigationController', donc une barre d'outils ajoutée manuellement était la seule façon de procéder. Merci! – codingFriend1

+1

Nice. Je pense que cela devrait être la réponse acceptée. Je voulais ** ajouter une barre d'outils à uitableviewcontroller **, ne pas activer uinavigationcontroller. – soemarko

Questions connexes