Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 导航控制器上的透明模态视图_Iphone - Fatal编程技术网

Iphone 导航控制器上的透明模态视图

Iphone 导航控制器上的透明模态视图,iphone,Iphone,我正在尝试在导航控制器的顶部创建一个透明的模式视图。有人知道这是否可行吗?我通过设置一个位于窗口或根视图的所有其他子视图之上的“OverlayViewController”最容易做到这一点。在应用程序委托或根视图控制器中设置此选项,并将OverlayViewController设置为单例,以便可以从代码或视图控制器层次结构中的任何位置访问它。然后,您可以随时调用方法来显示模式视图、显示活动指示器等,它们可能会覆盖任何选项卡栏或导航控制器 根视图控制器的示例代码: - (void)viewDidL

我正在尝试在导航控制器的顶部创建一个透明的模式视图。有人知道这是否可行吗?

我通过设置一个位于窗口或根视图的所有其他子视图之上的“OverlayViewController”最容易做到这一点。在应用程序委托或根视图控制器中设置此选项,并将OverlayViewController设置为单例,以便可以从代码或视图控制器层次结构中的任何位置访问它。然后,您可以随时调用方法来显示模式视图、显示活动指示器等,它们可能会覆盖任何选项卡栏或导航控制器

根视图控制器的示例代码:

- (void)viewDidLoad {
  OverlayViewController *o = [OverlayViewController sharedOverlayViewController];
  [self.view addSubview:o.view];
}
可能用于显示模式视图的示例代码:

[[OverlayViewController sharedOverlayViewController] presentModalViewController:myModalViewController animated:YES];
实际上,我还没有在OverlayViewController中使用
-presentModalViewController:animated:
,但我希望这可以正常工作


另请参见:

模式视图将覆盖推到其顶部的视图以及导航控制器的导航栏。但是,如果使用-presentModalViewController:animated:approach,则一旦动画完成,刚才覆盖的视图实际上将消失,这使得模态视图的任何透明度都毫无意义。(您可以通过在根视图控制器中实现-viewWillEnglishe:和-viewDidEnglishe:方法来验证这一点)

可以将模态视图直接添加到视图层次,如下所示:

UIView *modalView =
    [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
modalView.opaque = NO;
modalView.backgroundColor =
    [[UIColor blackColor] colorWithAlphaComponent:0.5f];

UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"Modal View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[label sizeToFit];
[label setCenter:CGPointMake(modalView.frame.size.width / 2,
                                modalView.frame.size.height / 2)];
[modalView addSubview:label];

[self.view addSubview:modalView];
像这样将modalView作为子视图添加到根视图实际上不会覆盖导航栏,但会覆盖其下方的整个视图。我尝试使用用于初始化modalView的帧的原点,但负值会导致它无法显示。除了状态栏之外,我发现覆盖整个屏幕的最佳方法是将modalView添加为窗口本身的子视图:

TransparentModalViewAppDelegate *delegate = (TransparentModalViewAppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window addSubview:modalView];

这篇关于的文章可能会对如何继续提供一些建议

通过在视图和导航栏上叠加透明/半透明按钮,可以实现透明/半透明模式视图效果

您可以通过UINavigationController的navigationBar属性访问导航栏


我发现UIButton与UILabel不同,它会捕获鼠标事件,从而给出正确的模式行为

对于导航或选项卡栏界面,在隐藏或显示基于此非常好的视图控制器之前,我最终通过组合一个隐藏/取消隐藏的覆盖视图控制器(请参见:pix0r的答案)完成了这一点


关于视图控制器,技巧是将其背景视图设置为
clearColor
,然后半透明覆盖视图可见,并且在视图控制器中作为子视图添加的任何视图都在前面,最重要的是不透明。

是的,您必须手动添加视图,如果你想从底部滑入,或者其他什么,你也必须自己做动画

为此,我编写了一个类,并以该类为例编写了一个半模态日期选择器


您可以在中找到文档,代码是

我也遇到了同样的问题,为了解决这个问题,解决方案是使用addSubview:添加模态视图,并使用UIView的animateWithDuration:延迟:选项:动画:完成:

我向UIViewController(FRRViewController)的子类添加了一个属性和两个方法,该子类包括其他功能。我将很快在gitHub上发布全部内容,但在此之前,您可以看到下面的相关代码。有关更多信息,请查看我的博客:


过去一周我一直在研究同一个问题。我尝试了在谷歌和StackOverflow上找到的各种答案和例子。没有一个工作得那么好

作为iOS编程新手,我不知道有什么叫做。因此,如果您试图实现这一点,以便显示按钮的模式覆盖(例如模式询问某人如何共享内容),只需使用
UIActionSheet


以下是。

以下是我为解决问题所做的工作-谷歌搜索详细信息,但这种方法对我非常有效:

  • 拍摄基本视图的屏幕截图这将导致一个现成的方法返回当前屏幕的UIView
  • 将屏幕截图交给模态视图(我使用了一个属性)
  • 呈现模态视图
  • 在模态视图控制器的ViewDidDisplay中,将图像设置为索引0处的UIImageView。通过状态栏的高度调整图像的垂直位置
  • 在模态视图控制器的视图中,将消失,再次删除图像
其效果是:

  • 与任何模态视图一样,视图在中设置动画-模态视图的半透明部分在现有视图上滑动
  • 动画一停止,背景就被设置为屏幕截图-这使它看起来好像旧视图仍然在下面,即使它不是
  • 模态视图的“消失”动画一开始,图像即被删除。同时,操作系统会显示旧的导航视图,因此模式视图会像您所期望的那样透明地滑离您的视线
我尝试在自己的叠加视图中设置动画,但效果不太好。我撞车了,没有任何迹象表明发生了什么事。而不是追查这一点,我做了背景视图和工程真的很好

模态视图中的代码-我认为您可以解决其余问题,即设置属性modalView.bgImage

- (void)viewDidAppear:(BOOL)animated {
    // background
    // Get status bar frame dimensions
    CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:self.bgImage];
    imageView.tag = 5;
    imageView.center = CGPointMake(imageView.center.x, imageView.center.y - statusBarRect.size.height);
    [self.view insertSubview:imageView atIndex:0];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self.view viewWithTag:5] removeFromSuperview];
}
这个主意是我从

准备: 在模态视图xib(或使用故事板的场景)中,我将全屏背景UIImageView设置为0.3 alpha(将其与.h文件挂钩,并为其提供属性“backgroundImageView”)。我将视图(UIView)的背景色设置为纯黑色

创意: 然后在模态视图控制器的“viewDidLoad”中,我捕获了原始状态a的屏幕截图
- (void)viewDidAppear:(BOOL)animated {
    // background
    // Get status bar frame dimensions
    CGRect statusBarRect = [[UIApplication sharedApplication] statusBarFrame];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:self.bgImage];
    imageView.tag = 5;
    imageView.center = CGPointMake(imageView.center.x, imageView.center.y - statusBarRect.size.height);
    [self.view insertSubview:imageView atIndex:0];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self.view viewWithTag:5] removeFromSuperview];
}
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
- (void) backgroundInitialize;
- (void) backgroundAnimateIn;
- (void) backgroundAnimateOut;
- (void) backgroundInitialize{
    UIGraphicsBeginImageContextWithOptions(((UIViewController *)delegate).view.window.frame.size, YES, 0.0);
    [((UIViewController *)delegate).view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    backgroundImageView.image=screenshot;
}
- (void) backgroundAnimateIn{
    CGRect backgroundImageViewRect = backgroundImageView.frame;
    CGRect backgroundImageViewRectTemp = backgroundImageViewRect;
    backgroundImageViewRectTemp.origin.y=-480;
    backgroundImageView.frame=backgroundImageViewRectTemp;

    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
        backgroundImageView.frame=backgroundImageViewRect;
    } completion:^(BOOL finished) {

    }];
}
- (void) backgroundAnimateOut{
    CGRect backgroundImageViewRect = backgroundImageView.frame;
    backgroundImageViewRect.origin.y-=480;

    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
        backgroundImageView.frame=backgroundImageViewRect;
    } completion:^(BOOL finished) {

    }];
}
[self backgroundInitialize];
[self backgroundAnimateIn];
[self backgroundAnimateOut];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:newview animated:YES];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalViewController animated:NO];
modalViewController.view.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
    modalViewController.view.alpha = 1;
}];
MyViewController * myViewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];
UINavigationController * myNavigationController = [[UINavigationController alloc] initWithRootViewController: myViewController];
myNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController: myNavigationController animated:YES];
UIViewController *modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor whiteColor] colorWithAlpha:0.5];
[self showDetailViewController:modalViewController sender:nil];
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.9f];
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:viewController animated:YES completion:nil];