ios - 滑入式菜单容器无法滑动

标签 ios swift

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 那样侧入和出。

最佳答案

代替我使用的 MainSideViewController 中的 sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width NotificationCenter.default.addObserver(self, 选择器: #selector(toggleSideMenu), 名称: NSNotification.Name("ToggleSideMenu"), 对象: nil)

并将其放入 @IBAction func profileButtonTapped(_ 发送者: 任何){ print("按钮被点击")

NotificationCenter.default.post(名称:NSNotification.Name(“ToggleSideMenu”),对象:nil) } }

关于ios - 滑入式菜单容器无法滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169305/

相关文章:

swift - 符合 Swift 协议(protocol) - 'Value of type XX has no member YY'

ios - 这段代码中的 'DispatchQueue.main.async' 和 'completed: @escaping () -> ()' 是什么意思?

ios - 为什么 swift 中的 var++ 是错误的,而改成 var+1 是正确的

objective-c - 如何以编程方式在 iPad 上显示导航 Controller 的下拉列表?

iOS,TableViewCell,动态更新单元格的高度

ios - 适用于 iOS 的 OpenGL 对象加载器

ios - 错误 :Unknown type name BOOL for c function in Xcode

ios - 创建 super 马里奥马赛克混合效果

swift - 谁在以正确的方式实现 ISO8601? swift 还是 Spring ?

ios - 在另一个 collectionview 的 header 中实现 collectionview 时出错