Question

I have an alertview with 3 buttons and text(label and NOT textView) If I jam it all together, it'll become ugly with this tiny scroll-text and all the buttons taking up most of the space. Does anyone know how to fix this?

Was it helpful?

Solution

Why don't you try to create a custom View for this.

You can use as you want to customize, size, color, background etc.

And show it as a modal window/view.


If you still want to use alertView then you can give spacing as:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Title" message:@"\n\n!\n\n\n\n" delegate:self cancelButtonTitle:@"Button 1" otherButtonTitles:@"Button 2",@"Button 3", nil];
[alert show];

OTHER TIPS

A simple way to move the text view down is to add a message

[alertViewObject setMessage:@"\n"];

THe reason for your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.

Customize your AlertView by using following Code.

// Customize Alert View
UIAlertView *alertView = [UIAlertView new];
alertView.title = @"Alert Title";


// Adding Your Buttons
[alertView addButtonWithTitle:@"Button1"];
[alertView addButtonWithTitle:@"Button2"];
[alertView addButtonWithTitle:@"Button3"];
[alertView addButtonWithTitle:@"Button4"];


// Add a Space for Text View
alertView.message = @"\n";


// View heirarchy, set its frame and begin bounce animation
[alertView show];


// Set the frame
CGRect frame = alertView.frame;
frame.origin.y -= 100.0f;
alertView.frame = frame;


// Adding TextField
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[alertView addSubview:text];
[text becomeFirstResponder]; 

I hope this will help you.

I suggest that you make two uiaertviews and then use

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

to check which alert view was clicked. If you really need an uialertview, else the answer below is also great.

Detailed:

  1. Add a uialertview delegate to your view controller:

    @interface ViewController : UIViewController <UIAlertViewDelegate>    
    
  2. When creating the 2 alertViews it should look like this:

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"the first UIAlertView" delegate: self cancelButtonTitle:@"Back" otherButtonTitle:@"More Options", nil];
    

and the second:

    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"More Options" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitle:@"Option 1", @"Option 2", @"Option 3", nil];
  1. In your ViewController.m add:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    
    if (alertView == alert1) {
      [alert2 show];
    }
    
    else if (alertView == alert2) {        
    
    NSString *string = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([string isEqualToString:@"Option 1"]) {
    
        //Do stuff
    
    }
    else if ([string isEqualToString:@"Option 2"]) {
    
        //Do something else
    }
    else if ([string isEqualToString:@"Option 3"]) {
    
        //Do something 3rd
    }
    }
    

UIAlertView was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    alertController.addAction(UIAlertAction(title: "AlertAction 0", style: .default, handler: { (alertAction: UIAlertAction) in
        print("0")
    }))

    alertController.addAction(UIAlertAction(title: "AlertAction 1", style: .default, handler: { (alertAction: UIAlertAction) in
        print("1")
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertAction: UIAlertAction) in
        print("cancel")
    }))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top