iosswiftxcodeside-menu

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


I have a design in which the sign-out button is placed at the bottom and should not scroll, side menu has a tableview controller in which we can add rows but my requirement is to add the sign-out button at the bottom. I have tried by adding the sign-out button in the footer view to side menu tableview controller, but showing just bellow the rows which I don't want.

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
    }
    
    
}

desired design enter image description here


Solution

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

    Next replace your function "getSignOutButton()->UIButton {}" with below code.

    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),
        ])
    }