Add button on top of UITableViewController (Swift)

前端 未结 8 1071
夕颜
夕颜 2021-02-07 15:34

I am trying to add a button ontop of a uitableview controller table view. The view controller has a navigation controller and static cells, which is why it is a uitableviewcontr

8条回答
  •  独厮守ぢ
    2021-02-07 16:04

    I did something similar with UITableViewController and a static datasource. I added the button in the footerview of my tableview.

    To make it align to the bottom of the screen i needed this code in my 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
        }
    

    In short, I resize the footerview to take up all the remaining space after the contentsize of the table view is removed. Since the button is aligned to the bottom of the footerView with autolayout, it will stay in the bottom of the screen.

    The Storyboard:

    Storyboard

    Here is the result:

    simulator

提交回复
热议问题