How to add subview inside UIAlertView for iOS 7?

I have an application in the iTunes store that displays some UILabel and UIWebView on a UIAlertView . According to the session video, addSubView for UIAlertView will not work. They talked about ContentView . But in the GM Seeds SDK, I could not find this property, and there seems to be no other way.

The only thing I can do is create a custom subclass of UIView and make it work like a UIAertView . Can you offer any other simple solution?

Thanks for the help.

+21
ios objective-c ios7 uialertview
Sep 12 '13 at 8:28
source share
6 answers

You can really change the accessory in customContentView in iOS7 (and, it seems, in iOS8) UIAlertView

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

Try this code:

 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]; 

Remember that before calling [alertView show]

You must install a custom accessory.

enter image description here

+99
Jan 11 '14 at 20:25
source share

AddSubview is not possible from iOS 7.

The only way is to create a custom subclass of UIView that can work like a UIAlertView. I could find several components on Github.

The linked Custom Alert seems to work well. By placing a proper version check, you can determine how to upgrade to your own UIAlertView or CustomAlertView.

+10
Sep 25 '13 at 11:50
source share

Adding subview to UIAlertView in iOS7 is different from previous versions of iOS. Try something like this:

 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 a workaround to create space inside the alert view [myAlertView addSubview:myCustomView]; } [myAlertView show]; //show the alert AFTER CUSTOMIZATION 
+3
Mar 21 '14 at 9:39
source share

Think you will also get help if you use it :)

 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]; 

Create a new UIProgressView view of 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]]; 

Another thing you have to do ...

 dispatch_async(dispatch_get_main_queue(), ^{ [self updateProgressBar]; }); 
-one
Oct 30 '13 at 5:32
source share

The custom kind of warning on github related to the accepted answer is wonderful. However, you cannot run this directly from the viewdidload method. The closing UIView is attached to the first window of the application, so you need to wait for split seconds. I added this code to viewdidload, which I found as a closed question.

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // Open the dialog here }); 
-2
Sep 16 '14 at 16:14
source share

The solution is to subclass UIAlertView and detect "_UIModalItemAlertContentView". My AlertView had a UITextField, so I used it to detect content:

 - (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: ... ]; } } 
-9
Jan 11 '14 at 12:33
source share



All Articles