如何在侧边菜单 swift 的底部添加一个按钮?

How to add a button at the bottom in sidemenu swift?

我有一个设计,其中退出按钮放置在底部并且不应滚动,侧面菜单有一个 tableview 控制器,我们可以在其中添加行,但我的要求是将退出按钮添加到底端。我尝试将页脚视图中的注销按钮添加到侧边菜单表视图控制器,但只显示在我不想要的行下方。

import UIKit
import SideMenu

class ProfileViewController: UIViewController {
    var menu:SideMenuNavigationController?
    override func viewDidLoad() {
        super.viewDidLoad()
        configureSideMenu()
    }
    
    
    
    func configureSideMenu() {
        menu = SideMenuNavigationController(rootViewController: MenuListController())
        menu?.navigationBar.setBackgroundImage(UIImage(named: "top_navbrBG"), for: .default)
        let firstFrame = CGRect(x: 20, y: 0, width: menu?.navigationBar.frame.width ?? 0/2, height: menu?.navigationBar.frame.height ?? 0)
            let firstLabel = UILabel(frame: firstFrame)
            firstLabel.text = "Settings"
        
        menu?.navigationBar.addSubview(firstLabel)
        SideMenuManager.default.addPanGestureToPresent(toView: self.view)
        
        
        
        let screenSize = UIScreen.main.bounds
        let screenHeight = screenSize.height + 40
        
        let leftBorderView = UIView(frame: CGRect(x: 1, y: -40, width: 1, height: screenHeight))
        leftBorderView.backgroundColor = UIColor.init(hexString: "#cfcfcf")
        menu?.navigationBar.addSubview(leftBorderView)
        
        
    }
    
    @IBAction func menuButtonAction(_ sender: UIButton) {
        if let menu = menu {
            present(menu, animated: true)
        }
    }
    

}

// functions
extension ProfileViewController {
    
    @objc func signOutButtonTapped(_ sender: AnyObject?) {
        print("sigin out")
    }
    func getSignOutButton()->UIButton {
        let button = UIButton()
        button.setTitle("Sign out", for: .normal)
        let color = UIColor.init(hexString: "#1A73E9")
        button.setTitleColor(color, for: .normal)
        button.addTarget(self, action: #selector(signOutButtonTapped), for: .touchUpInside)
        return button
    }
    
    func setConstraintsForSignOutButton(button: UIButton) {
        let screenSize = UIScreen.main.bounds
        let screenHeight = screenSize.height
        guard let menuTopAnchor = menu?.navigationBar.topAnchor else { return  }
        guard let menuLeadingAnchor = menu?.navigationBar.leadingAnchor else { return  }
        guard let menuTrailingAnchor = menu?.navigationBar.trailingAnchor else { return  }
        
        
        guard let menuWidth = menu?.menuWidth else { return  }
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.leadingAnchor.constraint(equalTo: menuLeadingAnchor, constant: 0),
            button.trailingAnchor.constraint(equalTo: menuTrailingAnchor, constant: 0),
            button.widthAnchor.constraint(equalToConstant: menuWidth),
            button.heightAnchor.constraint(equalToConstant: 40),
            button.bottomAnchor.constraint(equalTo: menuTopAnchor, constant: 300)
        ])
        
    }
}



// Menu Items
class MenuListController: UITableViewController {
    var menuItems = [[String: String]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        menuItems.append(["name": "Privacy", "img": "privacy", "key" : "privacy"])
        menuItems.append(["name": "Report issue", "img": "report_issue", "key": "report_issue"])
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        
        tableView.tableFooterView = getSignOutButton()
        
    }
    
    
    @objc func signOutButtonTapped(_ sender: AnyObject?) {
        print("sigin out")
    }
    func getSignOutButton()->UIButton {
        let button = UIButton()
        button.height = 20
        button.width = 100
        button.setTitle("Sign out", for: .normal)
        let color = UIColor.init(hexString: "#1A73E9")
        button.setTitleColor(color, for: .normal)
        button.addTarget(self, action: #selector(signOutButtonTapped), for: .touchUpInside)
        return button
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        menuItems.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if menuItems.indices.contains(indexPath.row) {
            let dict = menuItems[indexPath.row]
            cell.textLabel?.text = dict["name"]
            if let img = dict["img"] {
                cell.imageView?.image = UIImage(named: img)
            }
            
        }
        return cell
    }
    
    
}

想要的设计

Replace this "tableView.tableFooterView = getSignOutButton()" with
    self.getSignOutButton() in viewDidLoad.

接下来用下面的代码替换你的函数“getSignOutButton()->UIButton {}”。

func getSignOutButton() {
    let button = UIButton()
    button.setTitle("Sign out", for: .normal)
    button.setTitleColor(.blue, for: .normal)
    button.addTarget(self, action: #selector(signOutButtonTapped), for: .touchUpInside)
    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        button.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
        button.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
        button.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -20),
        button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
    ])
}