如何添加UIAlertView iOS 7内的子视图?

我在iTunes Store上有一个应用程序,在UIAlertView上显示一些UILabelUIWebView 。 根据会话video, UIAlertView addSubView将不起作用。 他们谈到了ContentView 。 但在通用种子SDK中,我找不到那个属性,似乎没有别的办法。

我唯一能做的就是创build一个UIView的自定义子类并使它像UIAertView一样UIAertView 。 你能build议任何其他简单的解决scheme?

谢谢您的帮助。

你真的可以在iOS7中将accessoryView更改为customContentView (在iOS8中也是如此)UIAlertView

 [alertView setValue:customContentView forKey:@"accessoryView"]; 

试试这个代码:

 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)]; [av setValue:v forKey:@"accessoryView"]; v.backgroundColor = [UIColor yellowColor]; [av show]; 

请记住,您需要在调用之前设置自定义的accessoryView [alertView显示]

在这里输入图像说明

AddSubview从iOS 7是不可能的。

唯一的方法是创build一个可以充当UIAlertView的自定义UIView子类。 我可以在Github上find几个组件。

链接的自定义警报似乎运作良好。 通过正确的版本检查,可以识别出原生的UIAlertView或CustomAlertView。

在iOS7中添加一个子视图到UIAlertView与早期的iOS版本是不同的。 尝试这样的事情:

 if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [myAlertView setValue:myCustomView forKey:@"accessoryView"]; //works only in iOS7 } else { myAlertView.message = @"\n"; //or just add \n to the end of the message (it's a workaround to create space inside the alert view [myAlertView addSubview:myCustomView]; } [myAlertView show]; //show the alert AFTER CUSTOMIZATION 

如果你使用它,想你也会得到帮助:)

 syncProgressBarView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault]; syncProgressBarView.frame =CGRectMake(20, 55, 250, 20); [syncProgressBarView setProgress:0.0f animated:NO]; syncProgressBarView.backgroundColor =[UIColor clearColor]; [progressView addSubview:syncProgressBarView]; 

UIProgressView子查看提供新的视图

 progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 100)]; [[UIApplication sharedApplication].keyWindow addSubview:progressView]; progressView.backgroundColor = [UIColor clearColor]; progressView.center = [UIApplication sharedApplication].keyWindow.center; syncProgressBarView.frame = CGRectMake(progressView.frame.size.width/2 - 250/2, progressView.frame.size.height/2 - 10, 250, 20); [[UIApplication sharedApplication].keyWindow bringSubviewToFront:[progressView superview]]; 

还有一件事你必须做…

 dispatch_async(dispatch_get_main_queue(), ^{ [self updateProgressBar]; }); 

在接受的答案链接在github上的自定义警报视图是伟大的。 但是,您无法直接从viewdidload方法启动。 封闭的UIView被附加到应用程序的第一个窗口,所以你必须等待一秒钟。 我将这个代码添加到我发现作为一个封闭的问题的viewdidload。

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // Open the dialog here }); 

一个解决scheme是inheritanceUIAlertView并检测“_UIModalItemAlertContentView”视图。 我的AlertView有一个UITextField,所以我用它来检测内容视图:

 - (void)show { [ super show ]; UIView *contentView=nil; if([[[UIDevice currentDevice] systemVersion] floatValue ] < 7.0f) { contentView=self; } else { UIView *rootView=[self textFieldAtIndex:0]; while((rootView=[rootView superview])!=nil) { if([ NSStringFromClass([rootView class]) isEqualToString:@"_UIModalItemAlertContentView"]) { contentView=rootView; break; } } } if(contentView!=nil) { [ contentView addSubview: ... ]; } }