Odpowiedz

49

Nie ma problemu, UITableViewController jest podklasą UIViewController. I tak się składa, że ​​w systemie iPhone OS 3.0 każda UIViewController(i podklasy) może działać w połączeniu z UINavigationController w celu zapewnienia kontekstowego paska narzędzi.

Aby to zadziałało musisz:

  • Upewnij się, że używasz UINavigationController zawierać wszystkie Państwa zdaniem kontrolerów że potrzebuje pasek narzędzi.
  • Ustawia właściwość toolbarsItems kontrolera widoku, który chce mieć pasek narzędzi.

To prawie tak proste, jak ustawienie tytułu kontrolera widoku i powinno być wykonane w ten sam sposób. Najprawdopodobniej przez przesłonięcie inicjatora initWithNibName:bundle:. Jako przykład:

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

Można również użyć setToolbarItems:animated: zamiast przypisywania do własności toolbarItems, dodawać i usuwać elementy paska narzędzi w animowanym mody w locie.

+0

Czy sterownik NavigationController jest wymagany? Chcę dodać ToolBar do TableViewController, który nie jest częścią NavigationController. Czy muszę używać kontrolera NavigationController, mimo że będzie on zawierał tylko jeden widok? –

+0

@sirjorj Tak, '' UINavigationController' jest wymagany, aby uzyskać obsługę * darmowego * paska narzędzi. Bez niego musisz zarządzać własną instancją widoku "UIToolbar". – PeyloW

+0

co, jeśli nie chcę umieszczać przycisków na tym pasku narzędzi, zamiast tego chcę umieścić tylko obraz w środku, co zrobiłbym inaczej? Dzięki. –

39

Aby przepis PeyloW do pracy, musiałem dodać następującą dodatkową linię kodu:

self.navigationController.toolbarHidden = NO; 

nadzieję, że pomoże ...

+2

Uzgodnione. Musiałem umieścić to wywołanie w metodzie viewDoLoad, a nie na overRide initWithNibName. Wtedy działa świetnie. –

+0

właśnie uratowałeś mój dzień, dziękuję –

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

    } 

aw 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

To działa doskonale dla mnie. Nie mogłem dodać 'UINavigationController', więc ręcznie dodany pasek narzędzi był jedyną dostępną opcją. Dzięki! – codingFriend1

+1

Nice. Myślę, że to powinna być akceptowana odpowiedź. Chciałem ** dodać pasek narzędzi do uitableviewcontroller **, nie włączając uinavigationcontroller. – soemarko

Powiązane problemy