Ios 创建粘贴到UITableView底部的UIView

Ios 创建粘贴到UITableView底部的UIView,ios,objective-c,Ios,Objective C,我有一个分组的UITableView,我想在我的UITableView的最底部添加一个UIButton。我正在使用故事板和UITableViewController。我不太确定我需要在哪里添加代码或拖放UI元素。我曾尝试在故事板中添加UIView作为footerview,然后在该视图中添加按钮,但这并没有达到始终停留在视图底部的预期效果,类似于选项卡。此外,按钮应始终位于最前面,UITableView应在其后面滚动 // tried this but it didn't work: self.t

我有一个
分组的UITableView
,我想在我的
UITableView
的最底部添加一个UIButton。我正在使用故事板和UITableViewController。我不太确定我需要在哪里添加代码或拖放UI元素。我曾尝试在故事板中添加UIView作为footerview,然后在该视图中添加按钮,但这并没有达到始终停留在视图底部的预期效果,类似于选项卡。此外,按钮应始终位于最前面,UITableView应在其后面滚动

// tried this but it didn't work:
self.tablview.tableFooterView = [[UIView alloc] initwithview...]

如果希望无论
UITableView
的滚动位置如何(即,不在
UITableView
内),您的
UITableView按钮都位于屏幕底部,那么您可能不应该使用
UITableViewController
子类。相反,如果使用
UIViewController
子类,只需将
UITableView
添加到根
view
属性中,并添加
UIButton
或其他具有适当布局约束的视图(如
UIToolbar
等),将其放置在屏幕底部或任何需要的位置。对于
UITableViewController
子类,这实际上是不可能的,因为root
view
属性必须是
UITableView
实例。

好的,如果要将工具栏直接添加到UITableViewController,可以按照中的说明创建“假”页脚视图

如果你对快速简单感兴趣,可以按照上面的@CharlesA给出答案,但是如果你使用一个带有UIBarButtonItem的标准工具栏,而不仅仅是一个圆形的Rect按钮,看起来会更好

如果您使用的是导航控制器,那么您只需取消隐藏工具栏(因为导航控制器已经提供了一个),然后添加一个按钮来执行所需操作。编码如下:

[self.navigationController setToolbarHidden:NO];

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle: @"Button Name"
                                            style: UIBarButtonItemStyleBordered
                                           target: self
                                           action: @selector(yourMethod:)];

self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, nil ];
我的方法是创建一个
UIViewController
,而不是
UITableViewController
。在VC中添加一个
TableView
,将TableView的底部抬高到足以容纳工具栏的位置,拖放一个工具栏,然后在其上拖放一个UIBarButtonItem。控件从UIBar按钮项中单击并创建您的
iAction


快速、简单、美观。

我在swift 1.2中通过使用TableViewController实现了这一点,方法如下(以防人们在寻找swift解决方案时遇到这种情况)

一旦故事板上有了TableViewController,请将视图拖到控制器上。根据需要进行设置,然后右键单击(或按住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)
然后

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)
    }

有一种方法可以在不使用UIViewController子类的情况下将视图添加到UITableViewController的底部

UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, footerY, footerWidth, footerHeight)];
[self.navigationController.view addSubview:footerView];

希望这有帮助

实际上,您可以添加UIButton以粘贴在视图的底部(或标题),无论您使用的是
UITableViewController
还是
UIViewController

斯威夫特3 创建按钮: 对于
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))
注意

查看宽度和高度如何
66
?好吧,你需要添加你的 按钮的高度和宽度的边距。在这种情况下,
50+16=66
。另外,通过这种方式添加背景色和其他属性 你的按钮是可见的

将视图添加到NavigationController
我相信您可以在Swift 2、1或Objective-C中找到解决方案。

我想为现有解决方案提供另一种解决方案。您可以在主视图中使用普通的
ViewController
,以便在主视图中显示粘滞视图。然后,将一个
容器视图
添加到
视图控制器
以显示表格,并添加另一个视图,该视图应成为粘滞视图

现在可以将
容器视图
指向您的
TableViewController
,并向粘滞视图添加约束,使其位于表格下方

因此,表不会与粘滞视图重叠,粘滞视图始终位于屏幕底部。此外,此概念不需要对现有的
TableViewController
进行任何更改


是的,可以在UITableViewConroller中使用。在UITableViewController类的viewDidLoad()函数中添加以下代码:

    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()

我做我的事时作弊了。我使用普通的ViewController,添加了一个表视图,使其比整个视图略短,然后在底部添加了一个带有按钮的工具栏。最简单的方法,但它可能会皱眉upon@CaptJak是的,基本上我想做的就是在我的UITableView上添加一个静态按钮…你可以使用你现在得到的答案,这与我问你是否想要的非常相似。如果您需要更详细的说明,那么我可以发布一个答案。@CaptJak我主要想知道这是否是最好的解决方案/最佳实践……您成功了吗?@CaptJak谢谢大家。关于这个方法的一个问题是:在故事板中,我会添加UIViewController,对吗?然后我只需添加UITableView(而不是UITableViewController),然后将这个新tableview的委托和数据源连接到UIViewController?对不起,我有点新,所以如果你能详细说明我将经历的确切过程,那将非常好=)@BobYousuk,这是正确的想法。您还必须在应用程序中实现适当的
UITableViewDataSource
/
UITableViewDelegate
方法
    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()