How to Add Subview Inside Uialertview for iOS 7

How to add subview inside UIAlertView for iOS 7?

You can really change accessoryView to customContentView in iOS7 (and it seems that in iOS8 as well) 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 you need set custom accessoryView before the call [alertView show]

Sample Image

Add UILabel Inside UIAlertView in iOS7

AddSubview is not possible from iOS 7.

How to add subview inside UIAlertView for iOS 7?

iOS 7 UIDatePicker in UIAlertView customisation

This is my component to support addSubview in AlertView.

CXAlertView - Custom alert-view which allow you to add view as main content.

Sample Image

How to add multiple UITextFields to a UIAlertView in iOS 7?

You can't do this any more, there is no addSubview for UIAlertView any more in iOS7.

Below are good alternative:

ios-custom-alertview

MZFormSheetController

UIAlertView Buttons(subviews) is not Showing in iOS 7

We can add subviews to UIAlerView by adding subview to the presentedViewController's view when UIAlertView is presented. I have accessed UIAlertView like following way :

NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews;

I have created a subclass of UIAlerView :

Header File :

@interface MLKLoadingAlertView : UIAlertView

- (id)initWithTitle:(NSString *)title;

@end

Implementation File :

#import "MLKLoadingAlertView.h"

#define ACTIVITY_INDICATOR_CENTER CGPointMake(130, 90)

@implementation MLKLoadingAlertView

- (id)initWithTitle:(NSString *)title
{
if ( self = [super init] )
{
self.title = title;
self.message = @"\n\n";

[self setDelegate:self];
}

return self;
}

// You can Customise this based on your requirement by adding subviews.
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews;

if( subviews.count > 1 )
{
// iOS while presenting an alertview uses a presening view controller. That controller's view has several subviews. I have picked one
// subview from it which has frame similar to the alertview frame.
UIView *presentedView = [subviews objectAtIndex:1];

UIActivityIndicatorView *customActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[customActivityIndicator startAnimating];
customActivityIndicator.center = ACTIVITY_INDICATOR_CENTER;

[presentedView addSubview:customActivityIndicator];
}
}

@end

In - (void)didPresentAlertView:(UIAlertView *)alertView method I have added the subviews to UIAlertView by accessing Presented View Controller's view.

You can find explanation and code example for this on Here



Related Topics



Leave a reply



Submit