How to add a toolbar to a TableView in iOS

可紊 提交于 2019-12-03 17:41:39

问题


I am building a simple notes application and I want to add a static bar at the bottom of the TableView. For example, I want to add a help button. How can I do this to just my TableView?

So far:

I have added a toolbar through storyboard, but that makes it stick at the end of the last made tableView cell. I want it stuck to the bottom. I entered this code to do programmatically:

@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;

in my tableViewController.h file and

[self.view addSubview:_toolbar];
[self.navigationController.view setFrame:self.view.frame];

in my tableViewController.m file in my viewDidLoad method

Thanks!


回答1:


The best solution is to use a UIViewController instead of a UITableViewController. (This has been said above, but let me give you the details).

Create a new UIViewController with it's respective XIB. Inside your new UIViewController's view drag in a UITableView, resize it, and drag your UIToolbar wherever you want.

You should have something like this:

The black border represents the UIViewController's main view. The red border represents the table view. The blue border represents your toolbar.

Afterwards, make your UIViewController comply with two protocols: UITableViewDelegate and UITableViewDataSource. You will manually have to implement it's essential methods such us cellForRowAtIndexPath, numberOfRowsInSection, etc, but it shouldn't take you long.

Link your UITableView to your UIViewController. Link it's "data source" and "delegate" properties to the view controller as well.

You will have your setup ready in less than 15 minutes.




回答2:


I just found a better trick!

  1. Make sure there is NO Navigation Bar (from your failed attempts)
  2. Drag and drop a "Bar Button Item" (Xcode will magically place it for you at the bottom)
  3. NOTE: If you Run the App now you won't see anything! (so keep reading)
  4. Add the following line of code under viewDidLoad:

    self.navigationController.toolbarHidden = NO;

Done!




回答3:


You can use a standard UITableViewController with a storyboard. In "simulated metrics", go to the Bottom Bar menu and select "Toolbar". The toolbar appears. You can then add button items to it.




回答4:


Add a toolbar via codes is simple.

UIToolbar *toobar = [[UIToolbar alloc]initWithFrame:frameYouNeed];
toobar.barStyle = UIBarStyleBlack;
[self.view addSubview:toobar];

But it would be better if you use UITableView as a subView, rather than a UITableViewController




回答5:


You should use ViewController instead of TableViewController

Then make your TableView as a IBOutlet in your header file. Declare delegate and data source. You are ready to go.




回答6:


You can use the toolbar that is included in all UITableViews by default, and add UIBarButtonItems to it. Despite the name, UIBarButtonItems can be customized to be any kind of view. Then you add them to the property self.toolbarItems, which is available to UITableViews by default. e.g.

UIBarButtonItem * textItem;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 48)];
label.text =  @"ImportantText";
label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = NSTextAlignmentRight;
textItem = [[ UIBarButtonItem alloc ] initWithCustomView:label];

UIBarButtonItem * switchItem;
UISwitch * switchCtrl = [[UISwitch alloc] init];
[switchCtrl addTarget:self action:@selector(toggleSomething:) forControlEvents:UIControlEventValueChanged];
switchItem = [[ UIBarButtonItem alloc ] initWithCustomView:switchCtrl];

self.toolbarItems = [ NSArray arrayWithObjects:
                     [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                     textItem,switchItem,nil ];



回答7:


I got this working in a Storyboard by checking Show Toolbar in the Navigation Controller. Then dragging Bar Button Items to the greyed out toolbar area now shown at the bottom of the Table View Controller. Then wiring the Bar Button Items up to methods Navigation Controller.




回答8:


Try this, It will add your toolbar as footerView of the table. and make sure you have give footer height of the table as 44(height of the toolbar).

self.tableView.tableFooterView = _toolbar;


来源:https://stackoverflow.com/questions/13061251/how-to-add-a-toolbar-to-a-tableview-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!