ios 创建一个粘在 UITableView 底部的 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18176843/
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
Creating a UIView that sticks to bottom of UITableView
提问by Bob Yousuk
I have a grouped UITableView
and I'd like to add a UIButton to the very bottom of my UITableView
. I'm using Storyboard and a UITableViewController. I'm not quite sure where I need to add code or drag/drop UI Elements. I've tried adding a UIView as the footerview in Storyboard and then adding a button to that view, but this doesn't have the desired effect of always staying on the very bottom of the view, similar to a tabbar. Also, the button should always stay in the forefront, and the UITableView should scroll behind it.
我有一个grouped UITableView
,我想在我的UITableView
. 我正在使用 Storyboard 和 UITableViewController。我不太确定需要在何处添加代码或拖放 UI 元素。我已经尝试在 Storyboard 中添加一个 UIView 作为页脚视图,然后向该视图添加一个按钮,但这并没有达到始终停留在视图最底部的预期效果,类似于标签栏。此外,按钮应始终位于最前面,而 UITableView 应在其后面滚动。
// tried this but it didn't work:
self.tablview.tableFooterView = [[UIView alloc] initwithview...]
回答by Charles A.
If you want your UIButton
to be at the bottom of the screen regardless of the scroll position of the UITableView
(i.e., not inside the UITableView
) then you should probably not use a UITableViewController
subclass. Instead, if you use a UIViewController
subclass, you can simply add a UITableView
to your root view
property and add a UIButton
or some other view (such as a UIToolbar
, etc) with appropriate layout constraints to place it at the bottom of the screen - or wherever you want it. This is not really possible with a UITableViewController
subclass, as the root view
property is required to be a UITableView
instance.
如果您希望UIButton
无论 的滚动位置如何UITableView
(即不在 内UITableView
)都位于屏幕底部,那么您可能不应该使用UITableViewController
子类。相反,如果您使用UIViewController
子类,您可以简单地将 a 添加UITableView
到您的根view
属性并添加具有适当布局约束的aUIButton
或其他视图(例如 aUIToolbar
等)以将其放置在屏幕底部 - 或您想要的任何位置. 这对于UITableViewController
子类来说是不可能的,因为根view
属性需要是一个UITableView
实例。
回答by CaptJak
OK, If you want to add the toolbar straight to the UITableViewController, you could follow the instructions in this tutorialto create a "fake" footer view.
好的,如果您想将工具栏直接添加到 UITableViewController,您可以按照本教程中的说明创建一个“假”页脚视图。
If you are interested in being quick and easy, follow the answer giver above by @CharlesA, but it would probably look nicer if you used a standard toolbar with a UIBarButtonItem instead of just a Round Rect Button.
如果您对快速简便感兴趣,请按照@CharlesA 上面的回答者进行操作,但如果您使用带有 UIBarButtonItem 而不是圆角矩形按钮的标准工具栏,它可能看起来会更好。
If you are using a Navigation Controller, then you can just unhide you toolbar (because the Navigation Controller comes with one already), and add a button to do what you need. Coding would be like this:
如果您使用的是导航控制器,那么您可以取消隐藏您的工具栏(因为导航控制器已经附带了一个),然后添加一个按钮来执行您需要的操作。编码将是这样的:
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle: @"Button Name"
style: UIBarButtonItemStyleBordered
target: self
action: @selector(yourMethod:)];
self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, nil ];
IMO the way to do it is create a UIViewController
instead of a UITableViewController
. Add a TableView
to the VC, raise the bottom of the TableView enough to fit a toolbar under there, drag and drop a toolbar, drag and drop a UIBarButtonItem onto it. Control-click from the UIBar button item and create your IBAction
.
IMO 的方法是创建一个UIViewController
而不是UITableViewController
. TableView
在 VC 中添加一个,将 TableView 的底部抬高到足以容纳下面的工具栏,拖放一个工具栏,将一个 UIBarButtonItem 拖放到它上面。按住 Control 单击 UIBar 按钮项并创建您的IBAction
.
Quick, simple, pretty.
快速,简单,漂亮。
回答by Amos Hsueh
There is one way to add views to the bottom of UITableViewController without using a UIViewController subclass.
有一种方法可以在不使用 UIViewController 子类的情况下将视图添加到 UITableViewController 的底部。
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, footerY, footerWidth, footerHeight)];
[self.navigationController.view addSubview:footerView];
Hope this help!
希望这有帮助!
回答by flash
I'd like to provide another solution to the existing ones. You can use a normal ViewController
for your main view where you want to have the sticky view in it. Then, add a container view
to the ViewController
to display the table and add another view which should become the sticky view.
我想为现有解决方案提供另一种解决方案。您可以ViewController
在主视图中使用法线,以便在其中放置粘性视图。然后,添加一个container view
以ViewController
显示表格并添加另一个应该成为粘性视图的视图。
It is now possible to point the container view
to your TableViewController
and add constraints to the sticky view to make it stay below the table.
现在可以将 指向container view
您的TableViewController
并将约束添加到粘性视图以使其位于表格下方。
As a result the table won't overlap with the sticky view and the sticky view always stays on the bottom of the screen. Furthermore, this concept does not required any changes in your existing TableViewController
.
因此,表格不会与粘性视图重叠,并且粘性视图始终位于屏幕底部。此外,此概念不需要对您现有的TableViewController
.
回答by Александр Зварич
Yes it's possible in UITableViewConroller:
是的,在 UITableViewConroller 中是可能的:
let bottomView = UIView()
bottomView.backgroundColor = .red // or your color
bottomView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 78, width: tableView.frame.size.width, height: 78) // 78 or your size of view
navigationController?.view.addSubview(bottomView)
tableView.tableFooterView = UIView()
回答by Farhad
Actually, you can add UIButton to stick in bottom (or header) of the view, it doesn't matter if you're using UITableViewController
or UIViewController
其实,你可以在视图的底部(或头)添加的UIButton坚持,这并不重要,如果你正在使用UITableViewController
或UIViewController
Swift 3
斯威夫特 3
Create Button:
创建按钮:
For X
and Y
I apply the entire view size, this will allow me to add margin with -
the amount I like. Then, just set size and width.
对于X
和Y
我申请的整个视图大小,这将让我有补充保证金-
我喜欢的量。然后,只需设置大小和宽度。
let myButton: UIButton = UIButton(CGRect(x: self.view.bounds.size.width - 66, y: self.view.bounds.size.height - 66, width: 50, height: 50))
Note
笔记
See how the width and height are
66
? Well, you need to add your margin to the height and width of the button. In this case,50 + 16 = 66
. Also, add you background color and other properties, that way your button is visible.
看看宽度和高度如何
66
?好吧,您需要将边距添加到按钮的高度和宽度上。在这种情况下,50 + 16 = 66
。此外,添加您的背景颜色和其他属性,这样您的按钮是可见的。
Add View to NavigationController
将视图添加到 NavigationController
self.navigationController?.view.addSubview(myButton)
I'm sure you can figure out how to do this in Swift 2, 1 or Objective-C.
我相信你可以弄清楚如何在 Swift 2、1 或 Objective-C 中做到这一点。
回答by Peter
I have achieved this with a TableViewController in swift 1.2 by doing the following (in case people come across this like I did while looking for swift solution).
我通过执行以下操作在 swift 1.2 中使用 TableViewController 实现了这一点(以防人们在寻找 swift 解决方案时遇到这种情况)。
Once you have your TableViewController on your storyboard drag a View onto the controller. Set it up as you desire and right-click(or ctrl-click) and drag into the source to create an IBOutlet. Then click and drag the view to the top bar . Change to the source for the TableViewController and you will need to derive UIScrollViewDelegate. You will need to set
tableView.delegate = self
and then you can do the following
一旦你在你的故事板上有你的 TableViewController 将一个 View 拖到控制器上。根据需要进行设置并右键单击(或按住 ctrl 单击)并拖动到源中以创建 IBOutlet。然后单击并拖动视图到顶部栏。更改为 TableViewController 的源代码,您将需要派生 UIScrollViewDelegate。您将需要设置
tableView.delegate = self
,然后您可以执行以下操作
self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, floatingView.frame.height, 0.0)
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, floatingView.frame.height, 0.0)
floatingView.frame.origin.y = self.tableView.bounds.origin.y + self.tableView.frame.height - floatingView.frame.height
floatingView.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin
self.view.addSubview(floatingView)
And then
进而
override func scrollViewDidScroll(scrollView: UIScrollView) {
floatingView.frame = CGRectMake(self.floatingView.frame.origin.x, self.tableView.bounds.origin.y + self.tableView.frame.height - floatingView.frame.height, self.floatingView.frame.size.width, self.floatingView.frame.size.height)
}