带有模态 UIViewController 的透明背景

发布于 2024-08-27 12:36:31 字数 333 浏览 12 评论 0原文

我有一个困境,我想向用户呈现一个半透明的视图。

我通过实验发现,如果我只是将透明视图推到导航控制器堆栈的顶部,它不会呈现我想要的透明度级别。因此,我决定简单地将视图添加为堆栈顶部当前视图的子视图。

该解决方案有效,下面的视图仍然可见,并且视图是“半模态”的。问题是,如果父视图继承自 UITableViewController (就像我的那样),那么我“推”到它上面的视图不会覆盖顶部的导航栏。

我真的不想陷入每次推送此视图时都被迫启用/禁用导航栏上的控件的情况,所以我想知道是否有人知道我可以使用的任何解决方案,以便该视图我推到 UITableViewController 上实际上会“推过”导航栏吗?

I have a dilema, I want to present to the user a semi-transparent view.

I found out by experimenting that if I simply pushed the transparent view to the top of my NavigationController's stack, that it would not render the transparency level I wanted. So I decided to simply add the view as a subview of the current view at the top of the stack.

This solution works, the view below is still visible, and the View is 'semi-modal'. The problem is, if the parent view inherits from UITableViewController (as mine does), then the view I 'push' onto it, does not cover the navigation bar at the top.

I really don't want to get into a situation where I am forced to enable / disable controls on the navigation bar every time I push this view, so I was wondering, if anyone knew of any solutions that I could use so that the view I push onto the UITableViewController will actually 'push over' the navigation bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泼猴你往哪里跑 2024-09-03 12:36:31

有趣的是,我昨天也在做同样的事情。不幸的是,这似乎是不可能的。一旦模态视图控制器就位,之前的视图就会隐藏。
请参阅有关该主题的上一个问题

您仍然可以使用已设置的视图控制器和 NIB 文件 - 这是我的示例代码

- (void)showUpgrade {
    [self.upgradeVC viewWillAppear:NO];
    [self.view addSubview:self.upgradeVC.view];
    [self.upgradeVC viewDidAppear:NO];
}

- (void)hideUpgrade {
    [self.upgradeVC viewWillDisappear:NO];
    [self.upgradeVC.view removeFromSuperview];
    [self.upgradeVC viewDidDisappear:NO];
}

- (UpgradeViewController *)upgradeVC {
    if (_upgradeVC == nil) {
        _upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:@"UpgradeView_%@", self.deviceType] bundle:nil];
        _upgradeVC.delegate = self;
    }
    return _upgradeVC;
}

您将需要在模式视图控制器中存储对父视图控制器的引用,以便您可以访问 -hide 方法。我通过一位代表做到了这一点。

如果你想让它从屏幕底部向上动画,向 -show-hide 添加一些动画也很容易 - 我只是懒得这么做这。

Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden.
See this previous question on the topic.

You can still use the view controller and NIB files you have set up - here's my sample code

- (void)showUpgrade {
    [self.upgradeVC viewWillAppear:NO];
    [self.view addSubview:self.upgradeVC.view];
    [self.upgradeVC viewDidAppear:NO];
}

- (void)hideUpgrade {
    [self.upgradeVC viewWillDisappear:NO];
    [self.upgradeVC.view removeFromSuperview];
    [self.upgradeVC viewDidDisappear:NO];
}

- (UpgradeViewController *)upgradeVC {
    if (_upgradeVC == nil) {
        _upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:@"UpgradeView_%@", self.deviceType] bundle:nil];
        _upgradeVC.delegate = self;
    }
    return _upgradeVC;
}

You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.

It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.

爱冒险 2024-09-03 12:36:31

iOS 8 添加了 UIModalPresentationOverFullScreen 演示风格。将其设置为呈现的视图控制器的 modalPresentationStyle。对于更高级的需求,请考虑创建自定义演示控制器

iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.

祁梦 2024-09-03 12:36:31

现在有一种方法可以使用 iOS7 自定义过渡来实现此目的:

MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];

要创建自定义过渡,您需要两件事:

  • 一个 TransitionDelegate 对象(实现
    )
  • 一个“AnimatedTransitioning”对象
    (实现

您可以在本教程中找到有关自定义过渡的更多信息:http://www.doubleencore.com/2013/09/ios-7-custom-transitions/

There is now a way to achieve this using iOS7 custom transitions :

MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];

To create your custom transition, you need 2 things :

  • A TransitionDelegate object (implementing
    <UIViewControllerTransitionDelegate>)
  • An "AnimatedTransitioning" object
    (implementing <UIViewControllerAnimatedTransitioning>)

You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/

暮色兮凉城 2024-09-03 12:36:31

试试这个:

ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];

Try this:

ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
森林迷了鹿 2024-09-03 12:36:31

您是否尝试过循环模态视图控制器的子视图并将每个视图的背景颜色设置为清除?这是一个DFS递归函数。

- (void)setBackgroundToClearForView:(UIView *)view {
    if ([view subviews]) {
        for (UIView *subView in [view subviews]) {
            [self setBackgroundToClearForView:subView];
        }
    }

    if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
        [view performSelector:@selector(setBackgroundColor:)
                   withObject:[UIColor clearColor]];
    }
}

要使用它,请

[self setBackgroundToClearForView:self.view];

viewDidLoad 中调用:。

Have you tried looping over the Modal View Controller's subviews and setting the background color to clear for every view? This is a DFS recursive function.

- (void)setBackgroundToClearForView:(UIView *)view {
    if ([view subviews]) {
        for (UIView *subView in [view subviews]) {
            [self setBackgroundToClearForView:subView];
        }
    }

    if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
        [view performSelector:@selector(setBackgroundColor:)
                   withObject:[UIColor clearColor]];
    }
}

To use it call:

[self setBackgroundToClearForView:self.view];

in viewDidLoad.

凤舞天涯 2024-09-03 12:36:31

这就能解决问题……试试这个。

// for clear color or you can easily adjust the alpha here
 YourVC *vc=[[YourVC alloc]initWithNibName:@"YourVC" bundle:nil] ;
 vc.view.backgroundColor = [UIColor clearColor];
 self.modalPresentationStyle = UIModalPresentationCurrentContext;
 [self presentViewController:vc animated:NO completion:nil];

这样视图将是全屏的,与 UIModalPresentationFormSheet 不同。

This will do the trick.. Try this one.

// for clear color or you can easily adjust the alpha here
 YourVC *vc=[[YourVC alloc]initWithNibName:@"YourVC" bundle:nil] ;
 vc.view.backgroundColor = [UIColor clearColor];
 self.modalPresentationStyle = UIModalPresentationCurrentContext;
 [self presentViewController:vc animated:NO completion:nil];

So that the view will be full screen unlike UIModalPresentationFormSheet..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文