Friday, 27 September 2013

Add Subviews inside UIAlertview in iOS7

Constants
#import "CustomIOS7AlertView.h"
#define alertTag 2
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Please find CustomIOS7AlertView .h and .m file Click here to download
Code for Custom UIAlertview subviews in iOS7 
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
   CustomIOS7AlertView *alert = [[CustomIOS7AlertView alloc] init];
   alert.backgroundColor = [UIColor whiteColor];
   [alert setContainerView:[self createAlertView:@"Check for iOS7 UIAlertview"]];
   [alert setButtonTitles:[NSMutableArray arrayWithObjects:@"Okay",@"Cancel",nil]];
   [alert setDelegate:self];
   alert.tag = alertTag;
   [alert show];
   [self.view addSubview:alert];
}else{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Cancel",nil];
   alert.tag = alertTag;
   [alert show];
}
- (UIView *)createAlertView:(NSString *)msg{
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
    demoView.layer.cornerRadius = 8.0f;
    demoView.layer.masksToBounds = YES;
    demoView.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 280, 30)];
    title.text = msg;
    title.textColor = [UIColor blackColor];
    title.backgroundColor = [UIColor clearColor];
    title.font =  [UIFont fontWithName:@"verdana" size:14];
    title.numberOfLines = 0;
    [demoView addSubview:title];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(20,50,38,30);
    [btn setImage:[UIImage imageNamed:@"circle-checked.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(toggleMe:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor clearColor];
    [demoView  addSubview:btn];
    UILabel *lblShare= [[UILabel alloc] initWithFrame:CGRectMake(70, 50, 190, 30)];
    lblShare.text=@"share on facebook";
    lblShare.numberOfLines=1;
    lblShare.textColor =[UIColor blackColor];
    lblShare.font=[UIFont fontWithName:@"verdana" size:14];
    lblShare.textAlignment=UITextAlignmentLeft;
    lblShare.backgroundColor =[UIColor clearColor];
    [demoView addSubview:lblShare];
    return demoView;
}
//Delegate
- (void)customIOS7dialogButtonTouchUpInside:(CustomIOS7AlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
 NSLog(@"Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
 NSInteger tag = alertView.tag;
 if (tag == alertTag){
 if (buttonIndex == 0)
 NSLog(@"pressed okay");
 else
 NSLog(@"pressed cancel");
 }
[alertView close];
}
// Button target
- (IBAction) toggleMe : (id) sender{
 UIButton *btn =  (UIButton *) sender;
 [btn setImage:[UIImage imageNamed:@"circle-unchecked.png"] forState:UIControlStateNormal];
}
//Output

2 comments: