回答

49

完全沒問題,UITableViewControllerUIViewController的一個子類。而且恰巧在iPhone OS 3.0中,任何UIViewController(和子類)都可以與UINavigationController一起使用以提供情境感知工具欄。

爲了這個工作,你必須:

  • 確保您使用UINavigationController包含需要一個工具欄所有視圖控制器。
  • 設置需要工具欄的視圖控制器的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; 
} 

你也可以用setToolbarItems:animated:,而不是分配給toolbarItems財產,以動畫方式在飛行中添加和刪除工具欄項目。

+0

是否需要NavigationController?我想將一個ToolBar添加到不屬於NavigationController的TableViewController中。即使只有一個視圖,我是否需要使用NavigationController? – 2011-05-25 04:53:17

+0

@sirjorj是的'UINavigationController'是必需的*免費*工具欄處理。沒有它,你必須管理你自己的'UIToolbar'視圖實例。 – PeyloW 2011-05-25 15:44:57

+0

如果我不想在此工具欄中放置按鈕,相反,我只想在中間放置一個圖像,那我會做什麼改變?謝謝。 – 2012-07-22 05:08:40

39

爲了使PeyloW的食譜工作,我需要添加以下代碼附加行:

self.navigationController.toolbarHidden = NO; 

希望幫助...

+2

同意。我必須將該調用放入viewDidLoad方法中,而不是initWithNibName重寫。然後它很好用。 – 2011-03-16 14:42:18

+0

你剛剛救了我一天,謝謝 – 2011-05-18 14:35:47

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

    } 

而且在斯威夫特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

這對我很好。我無法添加「UINavigationController」,因此手動添加工具欄是唯一的方法。謝謝! – codingFriend1 2012-11-28 11:24:41

+1

不錯。我認爲這應該是被接受的答案。我想**添加一個工具欄到uitableviewcontroller **,而不是啓用uinavigationcontroller。 – soemarko 2014-04-21 11:46:19