调用Popover时调暗主视图

时间:2012-10-30 12:35:47

标签: iphone objective-c ios xcode

有一个基本形式,按下按钮调用方法performSegueWithIdentifier,显示弹出窗口。在弹出窗口处于活动状态之前,我怎样才能使主视图窗口变暗(变暗)?

我试图像这样使用库SVProgressHUD

- (IBAction)publishButtonAction:(id)sender {
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    [self performSegueWithIdentifier:@"fbshareSigue" sender:self];
    [SVProgressHUD dismiss];
}

获得一瞬间 - 直到有一个弹出窗口。

如果我尝试这个,我必须在哪里插入此代码?:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier] isEqualToString:@"fbshareSigue"]) {
        OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
        UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
        svc.postBlock = self.postInitBlock;
        [svc setClosePopWindow:[popoverSegue popoverController]];
    }
}

    - (IBAction)publishButtonAction:(id)sender {
        UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        dimView.backgroundColor = [UIColor blackColor];
        dimView.alpha = 0.5f;
        dimView.tag = 1111;
        dimView.userInteractionEnabled = NO;
        [self.view addSubview:dimView];

        [self performSegueWithIdentifier:@"fbshareSigue" sender:self];
}

  /* This code should be put in the other method where you are removing the popup.. Because it will remove the dim view. here is the wrong place for this code*/  

 /* for (UIView *view in [self.view subviews]) {
        if (view.tag == 1111) {
            [view removeFromSuperview];
        }
    }*/

昏暗不起作用......

====================

我有调用Popover(OFAResultViewController)的查看OFAFBShareViewController

...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([[segue identifier] isEqualToString:@"fbshareSigue"]) {
        OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
        UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
        svc.postBlock = self.postInitBlock;
        [svc setClosePopWindow:[popoverSegue popoverController]];
    }
}

- (IBAction)publishButtonAction:(id)sender {
    UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    dimView.backgroundColor = [UIColor blackColor];
    dimView.alpha = 0.5f;
    dimView.tag = 1111;
    dimView.userInteractionEnabled = NO;
    [self.view addSubview:dimView];
    [self performSegueWithIdentifier:@"fbshareSigue" sender:self];    
}
...

OFAFBShareViewController我试图关闭昏暗的视图:

...
- (IBAction)cancelButtonAction:(id)sender {
    [closePopWindow dismissPopoverAnimated:YES];
    for (UIView *view in [self.view subviews]) {
            if (view.tag == 1111) {
                [view removeFromSuperview];
            }
}
...

但它不再起作用了......

2 个答案:

答案 0 :(得分:2)

显示弹出窗口时,您可以使用以下代码。

在OFAFBShareViewController中

- (void) loadView
{
       [super loadView]; 
       UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        dimView.backgroundColor = [UIColor blackColor];
        dimView.alpha = 0.5f;
        dimView.tag = 1111;
        dimView.userInteractionEnabled = NO;
        [self.view addSubview:dimView];
}

删除弹出窗口后,使用标记从超级视图中删除此视图。

在iOS 5.0之后,还引入了一种公共API方法:

=============================================== =======

要删除视图,您可以使用以下代码:

 for (UIView *view in [self.view subviews]) {
                if ([view.tag == 1111]) {
                    [view removeFromSuperview];
                }
            }

保持删除Dim View代码不变。仅将super.view再次更改为self.view

那对你有用。

希望它适合你...

答案 1 :(得分:0)

创建弹出窗口时,将昏暗视图添加到当前视图,并将当前视图添加为弹出控制器的UIPopoverControllerDelegate:

  1. 添加dimView:

    let dimView = UIView(frame: view.frame)
    view.addSubview(dimView)
    dimView.backgroundColor = UIColor.blackColor()
    dimView.alpha = 0.5
    dimView.tag = 1234
    dimView.userInteractionEnabled = false
    view.addSubview(dimView)
    
  2. 将原点视图添加为popoverController的委托:

    popoverViewController!.delegate = self
    
  3. 然后,为了在解除弹出窗口时删除dimView,将当前视图设置为实现UIPopoverControllerDelegate,并实现popoverControllerDidDismissPopover函数。在此函数内,删除dimView:

    extension MyOriginViewController: UIPopoverControllerDelegate {
    
        func popoverControllerDidDismissPopover(popoverController: UIPopoverController) {
            for subView in view.subviews {
                if subView.tag == 1234 {
                     subView.removeFromSuperview()
                }
            }
        }
    }