Iphone 创建黑色透明模态视图控制器

Iphone 创建黑色透明模态视图控制器,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我想创建一个具有黑色背景的模态视图控制器,但我想将alpha设置为0.9,这样我仍然可以看到它后面的部分视图。我该怎么做?我知道我可以使用UIView,然后将其作为子视图添加,但是如果我有UINavigationController或UIAbbarController,这不会覆盖整个屏幕。寻找一些关于这方面的建议,据我所知,到目前为止我所看到的解决方案从未涉及彩色背景。。大多数人只想要一个透明的背景。首先在.h文件中添加一个UIImageView UIImageView *overLayImag

我想创建一个具有黑色背景的模态视图控制器,但我想将alpha设置为0.9,这样我仍然可以看到它后面的部分视图。我该怎么做?我知道我可以使用UIView,然后将其作为子视图添加,但是如果我有UINavigationController或UIAbbarController,这不会覆盖整个屏幕。寻找一些关于这方面的建议,据我所知,到目前为止我所看到的解决方案从未涉及彩色背景。。大多数人只想要一个透明的背景。

首先在.h文件中添加一个UIImageView

UIImageView *overLayImage;
现在在viewDidLoad()上的.m文件中添加以下代码

其中“overlay1px.png”是您选择的颜色的透明图像,高度和宽度为1px X 1px

现在,在IBAction中,从您想要添加具有透明背景的视图的位置添加以下代码

[self.view addSubview:overLayImage];

[vwTermsCondition setFrame:CGRectMake(262, 300, 500, 120)];
[vwTermsCondition.layer setCornerRadius:5.0f];
[vwTermsCondition.layer setShadowColor:[UIColor blackColor]];
[vwTermsCondition.layer setMasksToBounds:YES];
[self.view addSubview:vwTermsCondition];
在上面的代码中,vwtermscondition是视图的名称。这适用于iPad应用程序,您可以调整iPhone的高度/宽度

享受:)

你试过了吗

[viewController1 presentModalViewController:viewController2 animated:YES];
我自己也没有试过,但文档上说它总是全屏显示。然后只需将其视图设置为具有
backgroundColor=[uicolorWithWhite:0.0alpha:0.9]。(白色=0.0表示黑色)。添加所需的任何子视图。或者将背景色设置为全黑色,将整个视图的alpha设置为0.9。这取决于您是否希望子视图也有点半透明。

将帮助您

您只需根据需要设置背景色。

我从

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

创意: 然后在模态视图控制器的“viewDidLoad”中,我从原始状态捕获屏幕截图,并将该图像设置为背景UIImageView。将初始Y点设置为-480,并使用0.4秒的持续时间和EaseInOut动画选项将其滑动到Y点0。当我们关闭视图控制器时,只需执行相反的操作

模态视图控制器类的代码

.h文件:

@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) {

    }];
}
.m文件:

@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) {

    }];
}
在viewDidLoad中,只需调用:

[self backgroundInitialize];
[self backgroundAnimateIn];
在我们关闭模态视图控制器的任何地方,我们调用:

[self backgroundAnimateOut];

请注意,这将始终设置背景图像的动画。因此,如果此模态视图控制器转换样式(或分段转换样式)未设置为“覆盖垂直”,则可能不需要调用动画方法。

因此,您不需要模态视图控制器?如果您有UINavigationController,则此操作将不起作用。。。因为它不会覆盖整个屏幕,这才是我真正想要的。。我想要一个模式视图控制器。不要只在主视图中添加一个视图,然后通过设置适当的边框从按钮显示它。@Nishant:如果不显示UINavigationController,这将起作用。如上所述,它将允许用户访问UIBarButtons,而这正是添加一层视图所必须停止的。