Add button on top of UITableViewController (Swift)

前端 未结 8 1069
夕颜
夕颜 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:22

    I find Container Views very useful in this scenario! A clean solution and very easy to implement.

    Just create a normal UIViewController, add your button and a ContainerView as subviews of this UIViewController (the middle one in the image below). Finally create Embed Segue from ContainerView to your UITableViewController (the one on the right).

    This way you can use static cell prototypes, not being limited only to UITableView at the same time.

    Result:

    0 讨论(0)
  • 2021-02-07 16:27

    Step 1 :-

    Drag and drop one uiview to UITable View Controller (Static) Automatically it sticks to the bottom.

    If you need to, you can also add two buttons inside UIView... It depends on your requirements.

    Step 2 :-

    Connect the outlet for uiview (outletView)

    Step 3 :-

    Add this below code in View Will Appear.

    override func viewWillAppear(_ animated: Bool) {
    
        outletViewBottom.backgroundColor = .red
        tableView.addSubview(outletViewBottom)
    
        // set position
        outletView.translatesAutoresizingMaskIntoConstraints = false
        outletView.leftAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.leftAnchor).isActive = true
        outletView.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor).isActive = true
        outletView.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor).isActive = true
        outletView.widthAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.widthAnchor).isActive = true
        outletView.heightAnchor.constraint(equalToConstant: 50).isActive = true // specify the height of the view
    
    }
    

    Step 4 :-

    Now run the code... Happy coding.

    0 讨论(0)
提交回复
热议问题