【问题标题】:Objective-C: Status Bar AlphaObjective-C:状态栏 Alpha
【发布时间】:2015-10-18 07:37:06
【问题描述】:

我正在创建一个自定义警报视图,并且我将此视图的背景设置为大部分为黑色,以使背景视图看起来略微褪色。这适用于状态栏(它保持完全相同)。

使用当前的 Apple AlertView 框架,当显示警报视图时,整个背景会略微淡化。如何复制此功能?

编辑

没有一个答案可以为我解决这个问题。这是我打开 AlertView 所做的事情:

[self.navigationController.view.superview addSubview:self.alertViewController.view];

然后从 viewDidLoad() 中的自定义警报视图控制器:

self.view.backgroundColor = COLOR_BLACK_ALPHA;

【问题讨论】:

  • 你为什么要在你的 superviews view? this is not how you deal with ViewController's, are you trying to add a childViewConroller` 中添加一个ViewController 视图?

标签: objective-c uialertview alpha


【解决方案1】:

你不能改变状态栏的 alpha,你只能设置它的外观。

UIAlertView 是一个 Apple 组件,因此它使用私有 API 来做一些你不能做的事情。

我的建议是,在显示您的视图之前,使用类似这样的方式拍摄其下方的屏幕快照(来源:Capture iPhone screen with status bar included

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];

比移除状态栏,并放置图像,模糊图像(可以使用模糊视图来完成,或者只是作为图像上的效果,然后显示您的视图。

如果您有任何问题,请提出。

【讨论】:

  • 它必须以某种方式成为可能,因为其他应用程序会这样做,例如 groupme。截图的问题是状态栏不会更新(即时间、充电信息等)。
  • @user3784214 我猜他们在所有其他窗口上方添加了一个窗口叠加层,并将其模糊,您无法直接访问状态栏 alpha。
  • 我想用windows实现类似的效果
【解决方案2】:

这个怎么样?

UIWindow *customWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
customWindow.windowLevel = UIWindowLevelStatusBar+1;
customWindow.hidden = NO;
customWindow.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
[customWindow makeKeyAndVisible];

现在在customWindow 中,您可以添加任何您想要的内容...

【讨论】:

  • 当我尝试以下没有任何反应: UIWindow *customWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; customWindow.windowLevel = UIWindowLevelStatusBar+1; customWindow.hidden = 否; customWindow.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7]; [自定义窗口 makeKeyAndVisible]; [customWindow addSubview:self.alertViewController.view];
  • @user3784214:请编辑您的问题并在此处发布代码...这里我看不懂...
【解决方案3】:

您可以通过在Window 中添加自定义叠加层来完成此操作,然后再添加自定义警报视图,如下所示:

UIWindow *aMainWindow = [[UIApplication sharedApplication] keyWindow];
self.grayOverlayView = [[MyCustomAlertViewOverlay alloc] initWithFrame:aMainWindow.bounds];
self.grayOverlayView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
[aMainWindow addSubview:self.grayOverlayView];
[aMainWindow addSubview:self.customAlertView];

这就是你的叠加层的样子:

@implementation MyCustomAlertViewOverlay


- (void)drawRect:(CGRect)iRect {
        CGContextRef aContext = UIGraphicsGetCurrentContext();
        CGContextSaveGState(aContext);
        CGColorRef aGradientStartColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0].CGColor;
        CGColorRef aGradientEndColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6].CGColor;
        NSArray *aColors = [NSArray arrayWithObjects:(__bridge id)aGradientStartColor, (__bridge id)aGradientEndColor, nil];
        CGFloat rLocations[2] = {0.0 , 0.5};
        CGColorSpaceRef rColorSpace = CGColorSpaceCreateDeviceRGB();
        CGGradientRef rGradient = CGGradientCreateWithColors(rColorSpace, (CFArrayRef) aColors, rLocations);
        CGColorSpaceRelease(rColorSpace);
        CGPoint aCenter = CGPointMake(iRect.origin.x + iRect.size.width / 2, iRect.origin.y + iRect.size.height / 2);
        CGContextDrawRadialGradient(aContext, rGradient, aCenter, 0, aCenter,  iRect.size.height, kCGGlyphMax);
        CGContextSetRGBFillColor(aContext, 0, 0, 0, 0.0);
        CGGradientRelease(rGradient);
        CGContextFillRect(aContext, iRect);
        CGContextRestoreGState(aContext);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2016-07-24
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多