presentViewController:animated:completion:不会将视图控制器动画化为视图



我正在调试下面代码中一个非常奇怪的问题:

if(condition1) {
   ImageViewController* imageViewer = [[ImageViewController alloc] initWithImageData:tappedItem];
   [self presentViewController:imageViewer animated:YES completion:^{
      [imageViewer loadImage];
   }];
}
else if(condition2) {
   DocumentViewController* docViewer = [[DocumentViewController alloc] init];
   [self presentViewController:docViewer animated:YES completion:nil];
}

也就是说,根据condition1condition2的状态,UIViewController的两个子类中的一个将以模态方式显示给用户。

在第二种情况下,一切都很好,但在第一种情况下,视图控制器没有呈现出通常的动画,显示它从屏幕底部滑动。相反,在短暂的延迟之后,它会突然出现,覆盖整个屏幕。另一个奇怪的地方是,在解雇动画中,视图控制器中的图像视图是透明的。

删除完成块没有效果。用UIViewController的实例替换我的视图控制器也没有效果,除了表明出于某种原因,动画也不适合UIViewController实例。

认为也许我在viewDidLoad等做错了什么,我试着评论出视图加载/出现的方法,但无济于事。

将视图控制器推到导航堆栈上不是一个选项,因为应用程序有一个选项卡栏,我不想让它可见。

DocumentViewController替换我的ImageViewController实例确实会产生动画。现在的问题是:我能在ImageViewController中做些什么来搞乱动画?

我已经找到了解决方案,但我仍然不知道真正的原因是什么。

修复方法是在viewDidload方法中为UIViewController模式显示的视图设置背景色,例如

self.view.backgroundColor = [UIColor grayColor];

如果我弄清楚到底发生了什么,我会在这里发布。

如何在标签栏控制器上显示视图控制器:

if(condition1) {
   ImageViewController* imageViewer = [[ImageViewController alloc] initWithImageData:tappedItem];
   [self.tabBarController presentViewController:imageViewer animated:YES completion:^{
      [imageViewer loadImage];
   }];
}
else if(condition2) {
   DocumentViewController* docViewer = [[DocumentViewController alloc] init];
   [self.tabBarController presentViewController:docViewer animated:YES completion:nil];
}

这也发生在我身上。改变背景颜色并没有什么帮助。我做了以下操作-结果非常好:

 -(void) viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:NO];
     self.view.userInteractionEnabled = FALSE;
     self.view.hidden = TRUE;
     self.navigationController.navigationBar.alpha = 0;
}
 -(void) viewDidAppear:(BOOL)animated
 {
     [super viewDidAppear:NO];
     float width = self.view.frame.size.width;
     float height = self.view.frame.size.height;
     self.view.frame = CGRectMake(0, height, width, height);
     self.view.hidden = FALSE;
     [UIView animateWithDuration:0.7f animations:^{
     self.view.frame = CGRectMake(0, 0, width, height);
    self.navigationController.navigationBar.alpha = 1;
} completion:^(BOOL finished){
    self.view.userInteractionEnabled = TRUE;
}];
 }

在iOS 8中设置背景色对我来说很有效。同时取消选中界面生成器工作中的不透明设置!

最新更新