【问题标题】:Transparent modalViewController [duplicate]透明 modalViewController [重复]
【发布时间】:2012-10-11 16:02:03
【问题描述】:

可能重复:
Transparent Modal View on Navigation Controller

我想用透明的backgroundColor 制作我的viewController,但我没有运气..

我尝试设置/使用...

  • self.view.backgroundColor = [UIColor clearColor]; - 我仍然有白色背景。
  • self.view.alpha = .0f; - 我在 VC 中的所有东西都不见了,但我仍然有白色背景
  • self.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:.7f] 我的透明视图下方仍然是白色背景。

编辑1:

测试日期:

  • iPad3、iOS 6.0
  • iPad 6.0 模拟器

【问题讨论】:

  • 您是否尝试将其设置为透明以便显示您呈现的视图?
  • 是的。下面的 ViewController 必须是可见的,当然是部分可见。

标签: objective-c ios


【解决方案1】:

经过更多研究,我发现了这种方法:

原码:

MyViewController *popUpViewController = [[MyViewController alloc] init];
[self presentModalViewController:popUpViewController animated:YES];
[popUpViewController release];

修改代码:

TTPopUpViewController *popUpViewController = [[TTPopUpViewController alloc] init];
popUpViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:popUpViewController animated:YES];
[popUpViewController release];

所以你可以看到我在我的 ViewController 上使用了 modalPresentationStyle 属性,我不想展示,就在调用 presentModalViewController:animated: 之前

注意:我的 ViewController 视图的大小为 {680, 700}

它以{540, 620} 的大小显示我的 ViewController 视图,并且由于我的原始尺寸更大,我隐藏了部分边框和阴影,但对我的视图外观和感觉几乎没有修改。

但是这种方法似乎给了我想要的东西。

========

有关更多信息,请参阅以下 Apple 文档:

UIViewController Class Reference

【讨论】:

    【解决方案2】:

    通常无法使用 presentModalViewController。这是一个类似的问题,有一个解决方法:

    Transparent Modal View on Navigation Controller

    【讨论】:

      【解决方案3】:

      您所做的问题是,在呈现新的 ViewController 后,透明视图下方的 ViewController 视图从屏幕上移除。在当前 ViewController 之上手动添加一个新视图并将其设置为透明也可以。

      【讨论】:

        【解决方案4】:

        您不能使用 presentViewController 来实现这一点,但是有一种方法 - 不是很优雅。

        .h file
        @propert(strong,nonatomic) MyCustomViewController *myCustomVC;
        
        .m file
        -(void) presentTransparentViewController
        {
        if(self.myCustomVC==nil) {
            _myCustomVC = [[MyCustomViewController alloc] init];
            }
        
        self.myCustomVC.view.backgroundColor = [UIColor clearColor];
        UIView *view = self.myCustomVC.view;
        view.opaque = NO;
        
        view.frame = self.view.frame; //assuming the views are the same size
        [self.view addSubview:view];
        }
        

        您还可以在主屏幕框架下方添加视图并使用动画呈现。

        【讨论】:

          【解决方案5】:

          我刚刚找到了解决方法。只需创建一个 1X1 的 UIViewController 并将其添加到您的父视图控制器。并在该 UIViewController 中显示模态视图控制器。

          在 viewDidLoad 上;

          self.dummyViewController = [[UIViewController alloc] init];
           [self.dummyViewController.view setFrame:CGRectMake(0, 0, 1, 1)];
           [self.view addSubView:self.dummyViewController.view];
          

          当你需要打开一个transparentViewController时;

          [self.dummyViewController presentModalViewController:yourTransparentModalViewController animated:true];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-10-26
            • 2016-05-19
            • 2016-11-01
            • 1970-01-01
            • 2013-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多