【问题标题】:How to show UIViewController in other UIViewController?如何在其他 UIViewController 中显示 UIViewController?
【发布时间】:2013-06-14 08:52:01
【问题描述】:

这是一个典型的问题,可能会重复,但是..

iPad 应用程序有UINavigationBarUITabBar。我在 navigationBar 上创建了一个按钮,该按钮必须显示 appinfoViewController

当我展示它时,navigationBar 和 tabTab 仍然可以点击。但我想将 appinfoViewController 显示为完整应用的主屏幕尺寸(如模态)

这是我的代码:

- (void)showInfo
{
AboutViewController *about = [[AboutViewController alloc] init];
[about.view setFrame: self.view.frame];
[about.view setAlpha:0.0];

[UIView animateWithDuration:0.5
                      delay:0.0
                    options: UIViewAnimationOptionCurveLinear
                 animations:^{
                     [about.view setAlpha:1.0];

                     [self.view addSubview:about.view];
                     [self addChildViewController:about];
                     [about didMoveToParentViewController:self];
                 }
                 completion:^(BOOL finished){

                 }];
}

如何将appinfoViewController呈现为全屏?

是否可以模态显示但没有黑色背景?

【问题讨论】:

  • addSubview 与 addChildViewController 不同。我的意思是:您可以实例化一个控制器,并将其视图(仅视图)添加到另一个控制器中。第一个控制器仍然存在并控制您的视图,即使它不在其原始控制器视图层次结构中。但是对于模态演示,您可以尝试:[self presentViewController:about animated:YES completion:nil]
  • 您是否尝试过presentViewController 将视图控制器呈现为模态?
  • 是的,但presentViewController 有黑色背景.. 但我需要透明度

标签: ios objective-c uiviewcontroller


【解决方案1】:

按照 cmets 的建议,使用

[self presentViewController:about animated:YES completion:nil]

将摆脱导航栏和标签栏。如果需要保留它们,则需要使用

[self.navigationController pushViewController:about animated:YES];

编辑:

为了在除关于视图之外的所有地方禁用用户交互,这有点棘手:首先,您需要将所有 UI 元素嵌入到一个不是呈现视图控制器的主视图的视图中。

假设您只有一个按钮(“显示”按钮),您不会只将它放在主视图中,而是使用另一个视图(我们称之为“外部视图”),就像与视图控制器的视图一样大,以及您放置按钮的位置(以及您可能拥有的任何其他 ui 元素)。你还需要这个外部视图的出口。然后写一个方法如:

-(void)userInteractionEnabled:(BOOL)enabled
{   
    self.navigationController.navigationBar.userInteractionEnabled = enabled;
    self.tabBarController.tabBar.userInteractionEnabled = enabled;
    self.outerView.userInteractionEnabled = enabled;
}

或者,您可以简单地禁用每个“交互式”插座而不是 outerView。因此,例如,如果您有 2 个文本视图、3 个按钮和一个 UIPickerView,那么您将为这些出口中的每一个设置 userInteractionEnabled = enabled(而不是只为父视图这样做)。

现在,在您的 showInfo 方法中,您可以:

 CGRect frame = CGRectMake(50, 50, 200, 200); //Use whatever origin and size you need
 about.view.frame = frame;
 [self.view addSubview:about.view];
 [self userInteractionEnabled:NO]

在您的 btnClose 方法中,您可以输入:

[about.view removeFromSuperview];
[self userInteractionEnabled:YES];

希望对您有所帮助,如果您需要,请告诉我!

附:也许您已经意识到这一点,但是有一个课程 UIPopoverController,仅适用于 iPad 的应用程序,它几乎可以为您完成所有这些工作。你可以在here找到使用教程。

【讨论】:

  • 第一个以黑色背景关闭(
  • 什么意思?
  • 如果我使用 [self presentViewController:about animated:YES completion:nil] 我的视图(大小为 300x200)出现在“parentView”中,但 300x200 周围都是黑色的!
  • 好的,我想我现在明白你想要做什么了。您希望这个关于视图位于另一个视图之上,并且仍然能够在这个 300x200 帧周围看到它?
  • 是的(对不起我的英语)))并且两者(navigationBar 或 tabBar)都不能用于点击
【解决方案2】:
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];

    AboutViewController *about = (AboutViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"about"];

[self.navigationController pushViewController:about Animation:YES];

【讨论】:

    【解决方案3】:

    您可以直接添加 viewcontroller 视图层来呈现 viewcontroller 视图。

    代码应该类似于 --

    AboutViewController *about = [[AboutViewController alloc] init];
    [about.view setFrame: self.view.bound];
    [about.view setAlpha:0.0];
    [self.view addSubview:about.view];
    
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [about.view setAlpha:1.0];
                     }
                     completion:^(BOOL finished){
    
                     }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多