【问题标题】:Slide-in menu container not sliding滑入式菜单容器不滑动
【发布时间】:2019-05-16 13:04:22
【问题描述】:

MainSideController 具有 toggleSideMenu 功能。但是当我点击 ContentViewController 中的配置文件 ButtonTapped 时。按钮显示它在调试菜单中被点击,但侧面菜单没有滑入。

import UIKit

class MainSideViewController: ContentViewController{
    @IBOutlet weak var sideMenuContainer: UIView!
    @IBOutlet weak var sideMenuViewLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var contentViewLeadingConstraint: NSLayoutConstraint!

    var sideMenuVisible = false

    override func viewDidLoad() {
        super.viewDidLoad()

        sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
    }

    @objc func toggleSideMenu(fromViewController: UIViewController) {
        if(sideMenuVisible){
            UIView.animate(withDuration: 0.5, animations: {

                self.sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width

                self.contentViewLeadingConstraint.constant = 0
                self.view.layoutIfNeeded()
            })
        } else {
            self.view.layoutIfNeeded()
            UIView.animate(withDuration: 0.5, animations: {

                self.sideMenuViewLeadingConstraint.constant = 0

                self.contentViewLeadingConstraint.constant = self.sideMenuContainer.frame.size.width
                self.view.layoutIfNeeded()
            })
        }

        sideMenuVisible = !sideMenuVisible
    }
}
import UIKit

class ContentViewController: UIViewController {
    let profileButton = UIButton(type: .custom)

    override func viewDidLoad() {
        super.viewDidLoad()

        profileButton.setImage(UIImage(named: "hamburger") , for: .normal)
        profileButton.contentMode = .scaleAspectFit
        profileButton.translatesAutoresizingMaskIntoConstraints = false

        // function performed when the button is tapped
        profileButton.addTarget(self, action: #selector(profileButtonTapped(_:)), for: .touchUpInside)

        // Add the profile button as the left bar button of the navigation bar
        let barbutton = UIBarButtonItem(customView: profileButton)
        self.navigationItem.leftBarButtonItem = barbutton

        // Set the width and height for the profile button
        NSLayoutConstraint.activate([
            profileButton.widthAnchor.constraint(equalToConstant: 35.0),
            profileButton.heightAnchor.constraint(equalToConstant: 35.0)
            ])

          // Make the profile button become circular
          profileButton.layer.cornerRadius = 35.0 / 2
          profileButton.clipsToBounds = true
    }

    @IBAction func profileButtonTapped(_ sender: Any){
        print("Button tapped")

        if let mainVC = self.navigationController?.tabBarController?  .parent as? MainSideViewController {
            mainVC.toggleSideMenu(fromViewController: self)
        }
    }
}

我将 sideMenuViewLeadingConstraint 添加到 viewDidLoad。 它仍然没有工作。我也添加了切换功能。它仍然没有滑入和滑出。

我希望配置文件按钮能够触发切换功能和侧边菜单,就像 twitter 边进边出一样。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    在我使用的 MainSideViewController 中代替 sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"), object: nil)

    并将其放入 @IBAction func profileButtonTapped(_ sender: Any){ print("点击按钮")

    NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil) } }

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多