Saturday, September 10, 2011

Customized AlertView

In this post, we will learn, How to

1. Change the frame size of UIAlertView
2. Change the frame/Location of buttons on UIAlertView
3. Align the Title label of UIAlertView in center.

Check the method below in code  willPresentAlertView

And don't forget to write - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  to handle the button click event of UIAlertview buttons.




- (void)willPresentAlertView:(UIAlertView *)alertView {
    
    

    if(alertView.tag==10)
    {
        //Set the frame size of Alertview
        alertView.frame = CGRectMake((1024-350)/2, (748-300)/2, 350, 200);
        
        //Align the title label
        UILabel *theTitle = [alertView valueForKey:@"_titleLabel"];
        theTitle.center=CGPointMake(170, 20);
        
        //Set the frame/location of buttons on alertview
        UIButton *cancelButton=[(NSMutableArray*)[alertView valueForKey:@"_buttons"] objectAtIndex:0];
        cancelButton.frame=CGRectMake(12, 125, 100, 45);
        
        UIButton *saveButton=[(NSMutableArray*)[alertView valueForKey:@"_buttons"] objectAtIndex:1];
        saveButton.frame=CGRectMake(124, 125, 100, 45);
        
        UIButton *laterButton=[(NSMutableArray*)[alertView valueForKey:@"_buttons"] objectAtIndex:2];
        laterButton.frame=CGRectMake(236, 125, 100, 45);


    }
    
}





-(void)showCustomizedAlertView
{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Enter your Info" message:@"\n\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save",@"Later" ,nil];
    alertView.tag=10;
    
    
    UILabel *labelA1=[[UILabel alloc]initWithFrame:CGRectMake(25,49,63,23)];
    [labelA1 setBackgroundColor:[UIColor clearColor]];
    [labelA1 setTextColor:[UIColor whiteColor]];
    [labelA1 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
    [alertView addSubview:labelA1];
    
    UILabel *labelA2=[[UILabel alloc]initWithFrame:CGRectMake(25,85,63,23)];
    [labelA2 setBackgroundColor:[UIColor clearColor]];
    [labelA2 setTextColor:[UIColor whiteColor]];
    [labelA2 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
    [alertView addSubview:labelA2];
    
    UILabel *labelA3=[[UILabel alloc]initWithFrame:CGRectMake(195,85,63,23)];
    [labelA3 setBackgroundColor:[UIColor clearColor]];
    [labelA3 setTextColor:[UIColor whiteColor]];
    [labelA3 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
    [alertView addSubview:labelA3];
    
    
    UILabel *labelA4=[[UILabel alloc]initWithFrame:CGRectMake(195,49,63,23)];
    [labelA4 setBackgroundColor:[UIColor clearColor]];
    [labelA4 setTextColor:[UIColor whiteColor]];
    [labelA4 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13]];
    [alertView addSubview:labelA4];
    
    
    
    [labelA1 setText:@"Name"];
    [labelA2 setText:@"Age"];
    [labelA3 setText:@"Sex"];
    [labelA4 setText:@"City"];
    
    [labelA1 release];
    [labelA2 release];
    [labelA3 release];
    [labelA4 release];
    
    //Add Them
    alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(92, 50, 80, 25)];
alertTextField.borderStyle = UITextBorderStyleRoundedRect;
alertTextField.backgroundColor = [UIColor clearColor];
alertTextField.placeholder = @" ";
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
    alertTextField.returnKeyType=UIReturnKeyDone;
[alertView addSubview:alertTextField];
[alertTextField becomeFirstResponder];
    
    
    alertTextField2 = [[UITextField alloc] initWithFrame:CGRectMake(92, 84, 80, 25)];
alertTextField2.borderStyle = UITextBorderStyleRoundedRect;
alertTextField2.backgroundColor = [UIColor clearColor];
alertTextField2.placeholder = @" ";
alertTextField2.keyboardType = UIKeyboardTypeNumberPad;
    alertTextField2.returnKeyType=UIReturnKeyDone;
[alertView addSubview:alertTextField2];
    
    
    
    alertTextField3 = [[UITextField alloc] initWithFrame:CGRectMake(245, 84, 80, 25)];
alertTextField3.borderStyle = UITextBorderStyleRoundedRect;
alertTextField3.backgroundColor = [UIColor clearColor];
alertTextField3.placeholder = @" ";
alertTextField3.keyboardType = UIKeyboardTypeNumberPad;
    alertTextField3.returnKeyType=UIReturnKeyDone;
[alertView addSubview:alertTextField3];
    
    
    
    alertTextField4 = [[UITextField alloc] initWithFrame:CGRectMake(245, 50, 80, 25)];
alertTextField4.borderStyle = UITextBorderStyleRoundedRect;
alertTextField4.backgroundColor = [UIColor clearColor];
alertTextField4.placeholder = @" ";
alertTextField4.keyboardType = UIKeyboardTypeNumberPad;
    alertTextField4.returnKeyType=UIReturnKeyDone;
[alertView addSubview:alertTextField4];
    
    
    [alertView show];
    [alertView release];
    
}

Output looks like following:













1 comment:

  1. Such articles are really good quality. I recommend everyone to read the end of every article on this blog.
    https://medical-interiors.com/de/consulting-services

    ReplyDelete