2017-05-03 2 views
0

ナビゲーションバーのメニューボタンを押したときに表示されるスライドインメニューがあります。それを却下するには、背景またはメニュー自体をタップするか、どちらかを実行します。このビューの中にはいくつかの項目があり、そのうちの1つはユーザープロファイルボタンです。このボタンを押すと、メニューを閉じてからユーザープロファイルビューコントローラーをすぐに開くことができます。だから私はhandleDismiss()をと呼んで開き、開く必要のあるビューを設定します。スウィフト - スライドインメニュー内からUIViewControllerを開きます。

しかし、ビューはウィンドウ階層内にないことを続けています。私は問題が何であるかを知っている(スタックを見る)が、どうにかして正しく動作するようにはできない。どのようにこの問題に取り組むべきですか?

RootViewController - >にHomeController(タブバーインデックス0) - >スライドのメニュー - コントローラ

profileCell.profileButton.addTarget(self, action: #selector(handleUserProfile(sender:)), for: .touchUpInside) 

機能

func handleDismiss() { 
    UIView.animate(withDuration: 0.5) { 
     self.blackView.alpha = 0 

     if let window = UIApplication.shared.keyWindow { 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 
     } 
    } 
} 

func handleUserProfile(sender: UIButton) { 
    handleDismiss() 

    let userProfileController = UserProfileController() 
    openProfileController(userProfileController) 
} 

func openProfileController(_ controller: UIViewController) { 
    present(controller, animated: false, completion: nil) 
} 
を開くため> UserProfileController

ボタン

答えて

0

固定私のメニューのrootViewControllerへの参照を設定して問題を解決してください。

にHomeController

let menuLauncher = MenuLauncher() 

func tappedMenu(sender: UIButton) { 
    menuLauncher.showMenu(self) 
} 

MenuController

func MenuLauncher() { 
    self.m_ParentViewController = nil 
} 

func showMenu(_ parentViewController: UIViewController) { 
    self.m_ParentViewController = parentViewController 

    collectionView.dataSource = self 
    collectionView.delegate = self 

    collectionView.register(MenuLauncherImageCell.self, forCellWithReuseIdentifier: cellIdImage) 
    collectionView.register(MenuLauncherInfoCell.self, forCellWithReuseIdentifier: cellIdInfo) 

    if let window = UIApplication.shared.keyWindow { 
     blackView.backgroundColor = UIColor(white: 0, alpha: 0.5) 

     blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss))) 

     window.addSubview(blackView) 
     window.addSubview(collectionView) 

     collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: 350) 

     blackView.frame = window.frame 
     blackView.alpha = 0 

     UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.blackView.alpha = 1 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height - 350, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 

     }, completion: nil) 
    } 
} 

func handleDismiss() { 
    UIView.animate(withDuration: 0.5) { 
     self.blackView.alpha = 0 

     if let window = UIApplication.shared.keyWindow { 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 
     } 
    } 
} 

func handleUserProfile(sender: UIButton) { 
    handleDismiss() 

    let userProfileController = UserProfileController() 
    self.m_ParentViewController?.present(userProfileController, animated: true, completion: nil) 
} 
関連する問題