2016-11-21 74 views
3

無法解除模態視圖控制器並返回到根視圖控制器。動畫確實顯示,但仍彈出當前的視圖控制器。無法解除模態視圖控制器

我正在開發的應用程序沒有使用storyboard,我想解僱當前的模式視圖控制器和回到根視圖控制器。

有沒有適當的方法來做到這一點?

我的根控制器是導航控制器雖然我的模式 - 視圖 - 控制器是的UIViewController。這是不工作的根本原因嗎?

模態視圖控制器(PlayerViewController)

func handleBack() { 
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil) 
} 

FeedCell.swift中的HomeController(FeedCell.swift)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let playerViewController = PlayerViewController() 
    playerViewController.modalPresentationStyle = .overCurrentContext 
    self.window?.rootViewController?.present(playerViewController, animated: true, completion: nil) 
} 

的AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    window = UIWindow(frame: UIScreen.main.bounds) 
    window?.makeKeyAndVisible() 

    let layout = UICollectionViewFlowLayout() 
    window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout)) 

    UINavigationBar.appearance().barTintColor = UIColor.rgb(red: 230, green: 32, blue: 31) 

    // get rid of black bar underneath navbar 
    UINavigationBar.appearance().shadowImage = UIImage() 
    UINavigationBar.appearance().setBackgroundImage(UIImage(),for: .default) 

    application.statusBarStyle = .lightContent 

    let statusBarBackgroundView = UIView() 
    statusBarBackgroundView.backgroundColor = UIColor.rgb(red: 194, green: 31, blue: 31) 

    window?.addSubview(statusBarBackgroundView) 
    window?.addConstraintsWithFormat(format: "H:|[v0]|", views: statusBarBackgroundView) 
    window?.addConstraintsWithFormat(format: "V:|[v0(20)]", views: statusBarBackgroundView) 

    return true 
} 

「抽頭」沒有顯示,但駁回不工作 enter image description here

+5

郵編,沒有想象 – Tj3n

+0

@ Tj3n完成編輯 – aznelite89

+0

代碼後重新添加按鈕,在'模式視圖控制器(PlayerViewController) ' – Vinodh

回答

1
let layout = UICollectionViewFlowLayout() 
let navController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout)) 
window?.rootViewController = navController 

試試這個。

+1

布拉沃,像魅力工作 – aznelite89

+0

謝謝,兄弟。有驚人的發展日。 –

1

如果你想關閉那些被提出作爲模態當前視圖控制器,你應該調用所呈現的視圖控制器的一個,並告訴它駁回呈現視圖控制器:

self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) 

presentingViewController

所呈現該視圖控制器視圖控制器。

+0

嗨感謝代碼,但它無法正常工作。 – aznelite89

+0

您應該直接從模態VC文件提供的視圖控制器調用該調用。 – OhadM

+0

仍然無法工作,仍然無法正常工作。我的根控制器是導航控制器,我的模式視圖控制器是UIViewController,它有可能是不工作的根本原因? – aznelite89

0

要消除任何的viewController,您應該使用

func handleBack() { 
     self.dismiss(animated: true, completion: {}) 
    } 
0

試試這個:

_ = self.navigationController?.popToRootViewController(animated: true) 
相關問題