ios - 如何在swift 4中将按钮放在表格 View Controller 的前面?

标签 ios uitableview uibutton swift4

这个问题在这里已经有了答案:





Add view over tableview (UITableViewController)

(7 个回答)


4年前关闭。




我有一个表格 View Controller ,对我来说唯一的方法是使用它,所以请不要告诉我使用 viewController 而不是 tableviewController 这是我添加按钮的代码,这个按钮显示在最后一个单元格中但是我想要当用户滚动 TableView 按钮位于屏幕底部而不移动

override func viewDidLoad() {
    super.viewDidLoad()


    let submitButton = UIButton(frame: CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50))

    submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
    submitButton.setTitle("Submit", for: .normal)
    submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
    submitButton.titleLabel?.textColor = .white
    submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
    submitButton.layer.cornerRadius = 5
    view.addSubview(submitButton)

最佳答案

如果适合您的场景,请尝试此方法:

var submitButton = UIButton()

override func viewDidLoad() {
    super.viewDidLoad()


    submitButton.frame = CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50)

    submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
    submitButton.setTitle("Submit", for: .normal)
    submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
    submitButton.titleLabel?.textColor = .white
    submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
    submitButton.layer.cornerRadius = 5
    self.view.window?.addSubview(submitButton)
}

但在这种情况下,您必须从 viewWillDisappear 中的 super View 中删除按钮。 :
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self. submitButton.removeFromSuperview()
}

关于ios - 如何在swift 4中将按钮放在表格 View Controller 的前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967134/

相关文章:

ios - imageView 设置为 UIViewContentModeScaleAspectFill 的 UIButton 不会完全填充小图像

iphone - UIButton 中的轮廓文本/字体

android - 可以通过智能手机 GPS 定位到我们所在的楼层吗?

ios - 当我检查玩家是否登录时 Gamecenter 崩溃

swift - Swift 中索引为 3 的 UITableViewEditingStyle

ios - UITableView可以上下拖动,不允许Swipe Gesture?

ios - 将保留的对象分配给不安全的属性

ios - UITableViewCell 按钮单击在 TableView 控件的多个部分中触发

ios - 具有清晰背景和彩色内容 View 的 UITableViewCell

iphone - 如何维护 UITableView 中特定按钮的状态(即停止覆盖图像)