最佳答案
如果屏幕上有 UITableView,可以通过创建自定义表头 View 来实现:
- 创建
UIView
的子类 ->CustomTableHeaderView
在其中添加一个按钮:
class CustomTableHeaderView: UIView { var rightButton: UIButton override init(frame: CGRect) { rightButton = UIButton() let buttonWidth: CGFloat = 100 // this is hardcoded for cimplicity, it's better to setup auto layout constraints here rightButton.frame = CGRect(x: 0, y: frame.width - buttonWidth, width: buttonWidth, height: 50) rightButton.setTitle("My button", forState: .Normal) // other configuration super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
- 在您的 View Controller 中(或您初始化 TableView 的任何其他地方,将您的自定义 View 设置为
tableHeaderView
:
tableView.tableHeaderView = CustomTableHeaderView(frame: frame:CGRect(x: 0, y: 0, width: view.frame.width, height: 10))
- 在您的 View Controller 中(或您初始化 TableView 的任何其他地方,将您的自定义 View 设置为
关于swift - 如何在表单的标题中创建自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333252/