繁体   English   中英

关于在 Swift 中的静态 tableviewController 中放置固定按钮的问题

[英]Question about putting a fixed button in a static tableviewController in Swift

我制作了一个带有静态表格视图的可滚动屏幕。

在此处输入图片说明

但我想创建一个独立于滚动此表视图的 UIButton。

我想将它锚定到视图的底部,如下所示。

在此处输入图片说明

是否可以在可滚动的静态 tableview 中将这样的按钮停靠在屏幕底部?

您应该为tableView创建一个页脚视图来执行此操作。

以编程方式

let footerView = UIView()
footerView.backgroundColor = .red // your color
// You have to give height and width yourself
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("<title>", for: .normal)
button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
footerView.addSubview(button)

通过接口生成器

只需将UIView ( CMD + Shift + L > type uiview) 拖放到 tableView 中,然后将按钮添加到footerView ,设置约束。


然后你应该把这个footerView添加到你的tableView

tableView.tableFooterView = footerView

为你的按钮创建一个动作

@objc func didTapButton(_ sender: UIButton) {
    print("didTapButton called")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM