SideMenu仅在具有Notification Center的第一个视图控制器中滑出

时间:2019-05-17 14:48:06

标签: ios swift

Sidemenu嵌入在mainviewcontroller中的容器中。 IBAction按钮位于contentviewcontroller中。侧面菜单可在Homeview控制器中使用,而不能在其他viewcontollers中使用

  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()

    //Side menu button Notification call
   NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"), object: nil)



    }


   @objc func toggleSideMenu(fromViewController: UIViewController) {
    if(sideMenuVisible){
        UIView.animate(withDuration: 0.5, animations: {
            // hide the side menu to the left
            self.sideMenuViewLeadingConstraint.constant = 0 -     self.sideMenuContainer.frame.size.width
            // move the content view (tab bar controller) to  original position
            self.contentViewLeadingConstraint.constant = 0
            self.view.layoutIfNeeded()
        })
      } else {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 0.5, animations: {
            // move the side menu to the right to show it
            self.sideMenuViewLeadingConstraint.constant = 0
            // move the content view (tab bar controller) to the  right
            self.contentViewLeadingConstraint.constant =  self.sideMenuContainer.frame.size.width
            self.view.layoutIfNeeded()
        })
        }

     sideMenuVisible = !sideMenuVisible
     }



        }


   import UIKit

   class ContentViewController: UIViewController {


   //Manipulation for Dashboard Side Menu Hamburger
   let profileButton = UIButton(type: .custom)

    override func viewDidLoad() {
    super.viewDidLoad()

     // Do any additional setup after loading the view.
     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){
    // current view controller (self) is embed in navigation controller which is embed in tab bar controller

       print("button pressed")

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


       }




  //The sidemenu is not coming out in this controller
     import UIKit

   class MyProfileViewController:  ContentViewController {

    override func viewDidLoad() {
    super.viewDidLoad()


      }




      }

当单击导航左栏按钮时,我希望Myprofileview也能滑出侧面菜单。但是只有嵌入式ViewControllerA中的第一个视图控制器会滑出。其他视图控制器不会滑出

0 个答案:

没有答案
相关问题