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

Posted

技术标签:

【中文标题】如何在侧边菜单的底部快速添加一个按钮?【英文标题】:How to add a button at the bottom in sidemenu swift? 【发布时间】:2021-08-27 03:26:58 【问题描述】:

我有一个设计,其中退出按钮放置在底部并且不应该滚动,侧面菜单有一个表格视图控制器,我们可以在其中添加行,但我的要求是在底部添加退出按钮.我尝试将页脚视图中的退出按钮添加到侧菜单 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
    
    
    

想要的设计

【问题讨论】:

添加为浮动按钮,您需要在superview上添加它并添加底部,尾随和按钮高度+宽度约束。 你能用代码解释一下吗 为什么要在 tableView 页脚中添加按钮? 其实我不怎么粘 添加了答案,请检查并告诉我。 【参考方案1】:
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),
    ])

【讨论】:

以上是关于如何在侧边菜单的底部快速添加一个按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式在 ios 的滑出式侧边栏菜单中添加按钮

快速关闭侧边菜单时如何更改为其原始视图颜色

如何一起使用底部导航栏和侧边导航栏,我的侧边导航按钮不显示,而是底部导航

如何在swift 4中消失没有按钮的侧边菜单

想要在一项活动中添加底部和侧边导航,但如何?

如何在侧边菜单中添加标题