Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView Buttons(subviews) is not Showing in iOS 7

UIAlertView is working fine in ios 6 with below code .But when it comes to ios 7 the subviews ( "yes" and "no" buttons in my code ) is not showing when alertview is called only text message is showing .Can anyone tell me how to resolve this problem ?

viewController.m file

 [Utilities prCustomAlert:@"Textmessage" inTitle:@"Alert view title" delegate:self inTag:300];
 CustomAlertView *alertView    = [Utilities sharedUtility].customAlertView;
alertView.numberOfBtns  = 2;
UIButton *btn= (UIButton *)[alertView viewWithTag:10];
[btn setTitle:@"no" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(dontlogout) forControlEvents:UIControlEventTouchDown];

btn = (UIButton *)[alertView viewWithTag:11];
[btn setTitle:@"yes" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchDown];    
[Utilities displayCustomAlertForDelegate:self];

UIAlertView.m file

  CGRect viewFrame    = self.frame;
  CGRect buttonFrame  = button.frame;
  if(self.numberOfBtns==2){

  CGRect labelFrame   = [self viewWithTag:15].frame;        
  button.frame    = CGRectMake(10, 0, 40, 30);
        button.hidden   = NO;

        //yes...
        btn          = (UIButton *)[self viewWithTag:11];       
        btn.frame    = CGRectMake(60, 0, 40, 30);
        btn.hidden   = NO;

        //no..    
        btn          = (UIButton *)[self viewWithTag:10];
        btn.hidden   = YES;


  }
like image 555
lreddy Avatar asked Oct 21 '22 00:10

lreddy


1 Answers

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

like image 108
Naga Mallesh Maddali Avatar answered Oct 24 '22 04:10

Naga Mallesh Maddali