iphone - 如何创建自定义 UIAlertView

标签 iphone objective-c cocoa-touch

我们正在创建一个沉浸式应用程序,它需要具有类似于 UIAlertView 的行为,但我们不希望它看起来像一个系统对话框,我们想使用我们自己的图形。我已经完成了大部分工作,但遇到了一些问题:

  1. 如何使 UIView 显示在状态栏上方(以便我可以变暗,就像 UIAlertView 一样)?这可能吗?我已将它添加到窗口,但它仍然显示在状态栏下方。

  2. 如何显示部分透明但文本仍然完全不透明?我希望它显示为类似于 UIAlertView,因为它应该是半透明的,但是如果我将 alpha 设置为 .8,这也会降低 subview 的 alpha。如果我只降低背景图像的 alpha,则按钮会显得不透明。如果我降低背景图像和按钮的 alpha,那么按钮看起来不像嵌入背景图像中。我非常希望不必为每个嵌入式按钮排列创建不同的图像

编辑:我还没有找到状态栏问题的解决方案,但我注意到标准的 UIAlertView 出现在它自己的 UIWindow 中,当我调查它时,我发现windowLevel 属性:

const UIWindowLevel UIWindowLevelNormal;
const UIWindowLevel UIWindowLevelAlert;
const UIWindowLevel UIWindowLevelStatusBar;

使用我自己的带有 UIWindowLevelAlert 的 UIWindow 仍然没有让它显示在状态栏上方。我没有尝试 UIWindowLevelStatusBar

最佳答案

您需要创建一个新窗口,您可以在其中放置警报 View :

UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // you must release the window somewhere, when you do not need it anymore
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertWindow.backgroundColor = [UIColor clearColor];
UIViewController *alertVC = [[[YourAlertViewController alloc] init] autorelease];
[alertWindow setRootViewController:alertVC];
[alertWindow setHidden:NO];

关于iphone - 如何创建自定义 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1765599/

相关文章:

ios - 将 ECSlidingViewController 与 TableView 一起使用

ios - 收到两个或多个通知时只执行一次选择器

iphone - 掌握 UIviews 和 UIViewcontrollers 的概念

iphone - 使用 afnetworking 在 json 有效负载中嵌入 base64 的正确方法

objective-c - 多个dispatch_async()完成时的回调

objective-c - 在 Swift 中从小图像生成纹理 UIImage

objective-c - 如何一次向 plist 中插入 200 多个条目?

ios - 滚动时 UItableView 弃用单元格

iphone - 动画-不断缩小的图像比例

iphone - 有多个 View 时如何禁用多点触控?