TableView HeaderSections隐藏按钮

时间:2018-10-04 19:11:35

标签: ios swift xcode uitableview

我一直在尝试解决tableviewcontroller遇到的问题。

tableviewcontroller中的部分是视图:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let sectionLabel = UILabel()
    sectionLabel.text = Catalog.sharedInstance.allSections[section - 1].name.localized(lang: defaults.string(forKey: "language")!)
    sectionLabel.font = UIFont(name: "Centabel Book", size: 25)
    sectionLabel.backgroundColor = UIColor.white
    sectionLabel.clipsToBounds = true

    return sectionLabel

}

如果我尝试以编程方式添加按钮以将其放在表格视图上

    let actionButton = JJFloatingActionButton()

    override func viewDidLoad() {
    super.viewDidLoad()

    // Configuration of the Floating Action Button
    actionButton.buttonColor = .red
    actionButton.addItem { item in
        self.performSegue(withIdentifier: "goToSettings", sender: nil)
    }
    actionButton.display(inViewController: self)
    actionButton.clipsToBounds = true
    // This doesn't work. It is to bring the button to the front. Now it is hidden by the sections.
    view.bringSubviewToFront(actionButton)

    // Checks if the language setting is nil, which means it is the first time you run the application. If then... selects English as default.
    if defaults.string(forKey: "language") == nil {

        defaults.set("en", forKey: "language")

    }

}

...我不知道为什么,但是viewForHeaderInSection隐藏了按钮。您可以在下图中检查它:

floating button hided by the headersection

我尝试使用该方法:

view.bringSubviewToFront(actionButton)

还有:

actionbutton.superview?.bringSubviewToFront(actionButton)

但是这些都没有把按钮带到前面。

我正在使用来自github的名为JJFloatingActionButton的浮动动作按钮。但是我尝试添加一个简单的UIButton,但出现了相同的错误。这是代码,也给了我viewDidLoad内部相同的错误:

        let button = UIButton(frame: CGRect(x: 100, y: 1000, width: 100, height: 50))
    button.backgroundColor = .green
    button.setTitle("Test Button", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

    self.view.addSubview(button)

再次出现相同的错误。您可以查看图片:

adding a simple UIButton happens the same problem

也许您可以帮我解决这个问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用常规的UIViewController(而不是UITableViewController)并手动添加UITableView

就像您可以更好地控制视图层次结构。