![](/img/trans.png)
[英]How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift?
[英]Presenting a ViewController breaks the previous ViewController
我的問題是,當我呈現一個UIViewController
,呈現的視圖會變黑。
我有一個名為mainViewController
的UIViewController
,它是我的Windows的根視圖。 里面有一個MMDrawerController
(只是作為mainViewController視圖的子視圖添加)。
MMDrawerController
包含我的其余視圖。
但是,當我從mainViewController
呈現一個新的UIViewController
,新的VC可以很好地顯示,但是當它關閉時,只留下了黑屏。 注意,添加時會出現黑屏(我可以直接看到它)。 解雇時不行。
為了進行測試,我編寫了以下代碼:
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc animated:NO completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
與正常使用相同的黑色結果。 (通常是QLViewController
...)
我的mainViewController設置如下:
_mainViewController = [MyMainViewController new];
_window.rootViewController = _mainViewController;
[self.window addSubview:_mainViewController.view];
在我的項目中更新MMDrawerController的代碼
我已經在MMDrawerController示例項目中測試了代碼,但是無法重現該問題,以下是我嘗試過的操作:
MMAppDelegate.m
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//...
UIViewController *testVc = [[UIViewController alloc] init];
testVc.view.backgroundColor = [UIColor greenColor];
[testVc.view addSubview:self.drawerController.view];
[self.window setRootViewController:testVc];
[self.window addSubview:testVc.view];
return YES;
}
MMExampleSideDrawerViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case MMDrawerSectionViewSelection:{
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
UIViewController *mainVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
[mainVC presentViewController:vc animated:YES completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
return;
//...
}
MMExampleCenterTableViewController.m:
-(void)doubleTap:(UITapGestureRecognizer*)gesture{
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor redColor];
UIViewController *mainVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
[mainVC presentViewController:vc animated:YES completion:^{
[vc dismissViewControllerAnimated:YES completion:nil];
}];
return;
[self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}
最后,問題是我在mainViewController上應用了插入約束。
在幕后,我想這個方法presentViewController:animated:completion:
使用框架,而不是自動版式,這打破了UIView
的的UIViewController
是在mainViewController。
感謝@simalone,我使用了相同的方法來查找問題的根源(使用MMDrawerController
示例項目)。
我想測試一下自己的問題, 我在示例項目中上傳了該問題,以便您理解。 在線上有一個斷點,它是錯誤的來源。 然后只需雙擊視圖。
再次感謝@simalone。 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.