double click to display slide menu!!

Hi IOS programmer,


I program an app that have slide menu by using continar and leading constant. The problem is when I click the button to slide the menu the menu didn't display but when I click again the menu displaied!! This code from https://github.com/Kilo-Loco/SideMenu/blob/master/Side%20Menu/MainVC.swift Hope anyone help me please. The following code display my problem.


class ViewControllerContainer: UIViewController {


@IBOutlet weak var sideMenuConstraint1: NSLayoutConstraint!

var sideMenuOpen = false

override func viewDidLoad() {

super.viewDidLoad()

NotificationCenter.default.addObserver(self,

selector: #selector(toggleSideMenu),

name: NSNotification.Name("ToggleSideMenu"),

object: nil) }


@objc func toggleSideMenu(_ sender: Any) {

if sideMenuOpen {

sideMenuConstraint1.constant = 236

sideMenuOpen = false

} else {

sideMenuConstraint1.constant = 0

sideMenuOpen = true

}

UIView.animate(withDuration: 0.3) {

self.view.layoutIfNeeded()

}

}

Replies

Where do you post ToggleSideMenu notification ?


When sideMenu is open, what should be the value of the constraint ?


Could you add print statements to trace :


    @objc func toggleSideMenu(_ sender: Any) {
        if sideMenuOpen {
            print("sideMenuOpen, former constraint", sideMenuConstraint1.constant)
            sideMenuConstraint1.constant = 236
            sideMenuOpen = false
        } else {
            print("sideMenu NOT Open, former constraint", sideMenuConstraint1.constant)
            sideMenuConstraint1.constant = 0
            sideMenuOpen = true
        }
        print("animation will begin")
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }



Also, for testing purpose, remove animation temporarily


    @objc func toggleSideMenu(_ sender: Any) {
        if sideMenuOpen {
            print("sideMenuOpen, former constraint", sideMenuConstraint1.constant)
            sideMenuConstraint1.constant = 236
            sideMenuOpen = false
        } else {
            print("sideMenu NOT Open, former constraint", sideMenuConstraint1.constant)
            sideMenuConstraint1.constant = 0
            sideMenuOpen = true
        }
        print("layout will begin")
        // REMOVE FOR TEST UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        // REMOVE FOR TEST}
    }

The original code you have shown, works as expected. So, you may be doing something wrong when posting the notification.


The behavior is something often found when you update UI components in the background thread. Aren't you posting the notification in the background thread?


Or you can try this code:

    @objc func toggleSideMenu() {
        DispatchQueue.main.async {
            if self.sideMenuOpen {
                self.sideMenuConstraint.constant = 236
                self.sideMenuOpen = false
            } else {
                self.sideMenuConstraint.constant = 0
                self.sideMenuOpen = true
            }
            UIView.animate(withDuration: 0.3) {
                self.view.layoutIfNeeded()
            }
        }
    }


Another possibility is your constant. The github code uses -240 to close and 0 to open. But you use positive number 236 to close.

Have you really set up your constraint as when the constant is 236 it's closed and when 0 open ?