在UITableViewController(Swift)上添加按钮

zac*_*ach 11 uibutton uitableview ios swift

我试图在uitableview控制器表视图的顶部添加一个按钮.视图控制器有一个导航控制器和静态单元,这就是为什么它是一个uitableviewcontroller而不是一个uiviewcontroller.现在我正在尝试在屏幕底部添加一个附加到导航控制器的按钮,这样它就不会随表视图一起滚动.

我想尝试做类似下面的事情.它有一个顶部栏的导航控制器,一个带静态单元格的表视图,然后是一个按钮,但他们是如何做按钮的呢?

图片:http://postimg.org/image/ilsmqqrip/

谢谢!

更新:如何使用带有使用Swift的静态单元格的tableview的uiviewcontroller?

小智 14

我发现Container Views在这种情况下非常有用!一个干净的解决方案,非常容易实现.

只需创建一个普通的UIViewController,添加你的按钮和一个ContainerView作为这个UIViewController的子视图(下图中的中间一个).最后从ContainerView创建Embed Segue到你的UITableViewController(右边的那个).

故事板

这样您就可以使用静态单元原型,而不仅限于UITableView.

结果:

结果


man*_*ukn 8

对此有更好的解决方案。您可以通过禁用相应 Button 或任何用于浮动按钮的 UIView 的 Auto Layout(button.translatesAutoresizingMaskIntoConstraints = false) 属性来执行此操作:

斯威夫特 4

//create a button or any UIView and add to subview
let button=UIButton.init(type: .system)
button.setTitle("NEXT", for: .normal)
button.frame.size = CGSize(width: 100, height: 50)
self.view.addSubview(button)

//set constrains
button.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
     button.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
     button.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
     button.rightAnchor.constraint(equalTo: tableView.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
     button.bottomAnchor.constraint(equalTo: tableView.layoutMarginsGuide.bottomAnchor, constant: -10).isActive = true
}
Run Code Online (Sandbox Code Playgroud)


knu*_*gro 5

我做了一些与UITableViewController静态数据源类似的东西.我在tableview的footerview中添加了按钮.

为了使它与屏幕底部对齐,我在viewcontroller中需要这个代码:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Make footerview so it fill up size of the screen
       // The button is aligned to bottom of the footerview 
       // using autolayout constraints
        self.tableView.tableFooterView = nil
        self.footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.tableView.frame.size.height - self.tableView.contentSize.height - self.footerView.frame.size.height)
        self.tableView.tableFooterView = self.footerView
    }
Run Code Online (Sandbox Code Playgroud)

简而言之,在删除表视图的内容后,我调整了footerview的大小以占用所有剩余的空间.由于按钮与footerView的底部对齐并具有自动布局,因此它将保留在屏幕的底部.

故事板:

故事板

结果如下:

模拟器


小智 -1

这是一个 UIViewController,其中添加了一个 UITableView 作为子视图。在右上角,您可以看到一个下拉菜单,内容为:动态原型。将其更改为静态单元格。在此输入图像描述