ios 如何在故事板中的 UITableViewController 底部添加工具栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19625416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a toolbar to the bottom of a UITableViewController in Storyboards?
提问by vzm
In my UITableView
that I have setup using Storyboards, I need to be able to add a tool bar that sticks to the bottom of the view, it should not scroll.
在我UITableView
使用 Storyboards 进行设置的情况下,我需要能够添加一个贴在视图底部的工具栏,它不应该滚动。
Unlike this question: LINKI don't think I could add a TableView subview to a normal view and then just add a toolbar programmatically because I am using dynamic cells which seem a lot easier to integrate via Storyboards.
与这个问题不同:LINK我不认为我可以将 TableView 子视图添加到普通视图,然后只以编程方式添加工具栏,因为我使用的是动态单元格,这似乎更容易通过故事板集成。
For now, this is what I am stuck with....
现在,这就是我所坚持的......
回答by Zoxaer
if you want show toolbar in one view controller which placed in some navigation controller.
如果您想在放置在某个导航控制器中的一个视图控制器中显示工具栏。
- select view controller in storyboard
- in utilities, show "attribute inspector". select "bottom bar" style.
- add bar button item
- add code in view controller, to show and hide toolbar:
- 在故事板中选择视图控制器
- 在实用程序中,显示“属性检查器”。选择“底栏”样式。
- 添加条形按钮项
- 在视图控制器中添加代码,以显示和隐藏工具栏:
code:
代码:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
回答by Kegham K.
回答by Keith Holliday
For Swift users, you can use the following code:
对于 Swift 用户,您可以使用以下代码:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setToolbarHidden(false, animated: animated)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated);
self.navigationController?.setToolbarHidden(true, animated: animated)
}
回答by Josh
This remedy works for (2016) iOS 9.2. We all hate how Apple makes us waste time in stuff that should be straightforward like this. I like step by step solutions for this type of silly problems, so I will share it with you!:
此补救措施适用于 (2016) iOS 9.2。我们都讨厌 Apple 如何让我们浪费时间在像这样简单的事情上。我喜欢这种类型的愚蠢问题的逐步解决方案,所以我将与您分享!:
- Select your View controller > Attribute Inspector > Select "Opaque Toolbar"
- Now, drag and drop a "Bar Button item to your Storyboard.
- Select your newly dropped Bar Button Item > Atrribute Inspector > System Icon > Select your favorite icon.
In the viewDidLoad() method of your View controller, add this code before anything else:
override func viewDidLoad(animated: Bool) { self.navigationController?.setToolbarHidden(false, animated: true)
//the rest of code }
You don't want that toolbar hanging around elsewhere, so add this inside your view to hide it once the current window is dismissed:
- 选择您的视图控制器 > 属性检查器 > 选择“不透明工具栏”
- 现在,将“条形按钮”项目拖放到故事板中。
- 选择您新放置的 Bar Button Item > Atrtribute Inspector > System Icon > 选择您喜欢的图标。
在视图控制器的 viewDidLoad() 方法中,在其他任何内容之前添加以下代码:
override func viewDidLoad(animated: Bool) { self.navigationController?.setToolbarHidden(false, animated: true)
//其余代码 }
您不希望该工具栏挂在其他地方,因此在当前窗口关闭后将其添加到您的视图中以隐藏它:
-
——
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated);
self.navigationController?.setToolbarHidden(true, animated: animated)
}
Voila!
瞧!
回答by David Wong
- Drag a UIViewController into Storyboard
- Drag a UIToolbar on top of the Storyboard's contents.
- Drag a UITableView on top of the Storyboard's contents.
- Link the tableview's delegate and datasource to your source code.
- 将 UIViewController 拖入 Storyboard
- 将 UIToolbar 拖到 Storyboard 内容的顶部。
- 将 UITableView 拖到 Storyboard 的内容之上。
- 将 tableview 的委托和数据源链接到您的源代码。
Although you won't be able to use UITableViewController as your linking class step 4 will allow you to link it to a regular UIViewController.
尽管您将无法使用 UITableViewController 作为您的链接类第 4 步将允许您将其链接到常规 UIViewController。
You'll need something like this in the header though
虽然你需要在标题中这样的东西
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
It'll look something like this in your storyboard:
它在你的故事板中看起来像这样:
回答by Brian Broom
I used an intermediate View Controller with a Container view to the table. Add the toolbar view to the intermediate, and make it look however you want (use UIButtons instead of UIBarButtonItem).
我使用了一个中间视图控制器和一个容器视图到表。将工具栏视图添加到中间,并使其看起来像你想要的(使用 UIButtons 而不是 UIBarButtonItem)。
If you do this, have the container view stretch to the top of the screen and not the bottom of the nav bar or you'll pull your hair out trying to get the scroll insets right.
如果您这样做,请将容器视图拉伸到屏幕顶部而不是导航栏的底部,否则您会试图使滚动插入正确。
Some more details in a similar question https://stackoverflow.com/a/31878998/1042111
类似问题中的更多详细信息https://stackoverflow.com/a/31878998/1042111