Add button on top of UITableViewController (Swift)

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

提交回复
热议问题