【问题标题】:iPhone modal View with parent view semi-visible?iPhone模态视图与父视图半可见?
【发布时间】:2010-05-16 18:48:40
【问题描述】:

我正在使用以下代码添加模式视图:

[self presentModalViewController:phrasesEditor animated:YES];

如何使模态视图半透明,以便超级视图“发光”?

我的完整方法/功能如下所示:

-(IBAction)showEditPhrases:(id)sender{
    PhrasesViewController *phrasesEditor = [[PhrasesViewController alloc] initWithNibName:@"PhrasesViewController" bundle:nil];
    phrasesEditor.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [phrasesEditor.view setAlpha: 0.5];
    [phrasesEditor.view setBackgroundColor: [UIColor clearColor]];
    [self presentModalViewController:phrasesEditor animated:YES];
    [phrasesEditor release];
}

编辑:

显然,更改 alpha 的务实方法行不通。如何将 NIB 加载到 UIView 并与之交互?

我现在有一个 UIViewController。我是将其转换/修改/更改为 UIVIew 然后加载它,还是我应该做其他事情?

编辑 2:

我尝试了[self.view addSubview:phrasesEditor.view];,但这让我无法删除子视图。每个视图似乎都有自己的视图控制器。

编辑 3:

我想我应该提一下,超级视图位于名为 iDecideViewController 的视图控制器内,而短语编辑器有一个单独的视图控制器。

【问题讨论】:

    标签: iphone modal-dialog


    【解决方案1】:

    我遇到了同样的问题,并在 UIViewController 的子类中添加了一个属性和 2 个方法来模仿 presentModalViewController:animated 的行为:

    有关更多信息,请参阅我的博文:How to display a transparent modal view controller 或查看以下代码:

    #pragma mark - Transparent Modal View
    -(void) presentTransparentModalViewController: (UIViewController *) aViewController 
                                         animated: (BOOL) isAnimated 
                                        withAlpha: (CGFloat) anAlpha{
    
        self.transparentModalViewController = aViewController;
        UIView *view = aViewController.view;
    
        view.opaque = NO;
        view.alpha = anAlpha;
        [view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            UIView *each = obj;
            each.opaque = NO;
            each.alpha = anAlpha;
        }];
    
        if (isAnimated) {
            //Animated
            CGRect mainrect = [[UIScreen mainScreen] bounds];
            CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
    
    
            [self.view addSubview:view];
            view.frame = newRect;
    
            [UIView animateWithDuration:0.8
                             animations:^{
                                 view.frame = mainrect;
                             } completion:^(BOOL finished) {
                                 //nop
                             }];
    
        }else{
            view.frame = [[UIScreen mainScreen] bounds];
            [self.view addSubview:view];
        }
    
    
    
    
    
    
    }
    
    -(void) dismissTransparentModalViewControllerAnimated:(BOOL) animated{
    
        if (animated) {
            CGRect mainrect = [[UIScreen mainScreen] bounds];
            CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
            [UIView animateWithDuration:0.8
                             animations:^{
                                 self.transparentModalViewController.view.frame = newRect;
                             } completion:^(BOOL finished) {
                                 [self.transparentModalViewController.view removeFromSuperview];
                                 self.transparentModalViewController = nil;
                             }];
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

      当你呈现一个模态视图时,它实际上会卸载前一个视图,所以这根本不是你想要的。 UIView 有一个名为 addSubview 的方法,它将新视图置于其兄弟视图之上。

      代替:

      [self presentModalViewController:phrasesEditor animated:YES];
      

      做:

      [self.view addSubview:phrasesEditor.view];
      

      这是假设您已经在 ViewController 子类中。

      【讨论】:

      • removeFromSuperview on phrasesEditor.view
      • 我不确定。它有一个独立于短语编辑器的视图控制器并导致 EXC_BAD_ACCESS
      • 编辑:我想通了。我[phraseEditor release] 太早了。
      • 所以现在我的内存泄漏了,我得解决这个问题。
      • 照顾。我决定坚持使用[self presentModalViewController:phrasesEditor animated:YES]; 并通过 UIImage 为其应用背景图像。问题解决了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      相关资源
      最近更新 更多