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?
4 Answers
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];

- 46,283
- 15
- 111
- 140
-
Thanks for a great answer, but since I'm new to Objective-C, I haven't started using multiple views, but still great answer (wish I could vote up..) – user1970792 Jan 11 '13 at 18:00
-
I tried it but the thing is that I need to have to display some information, but thanks – user1970792 Jan 11 '13 at 18:14
-
`Some information` where in place of `!` ? And where you want to put ! ? – Anoop Vaidya Jan 11 '13 at 18:15
-
You have only two places to show your texts (title and message). – Anoop Vaidya Jan 11 '13 at 18:16
-
Yes, sorry for my non-clearness, I meant to display a message (not title) And btw, sorry for not understanding that "!" was supposed to be my message – user1970792 Jan 11 '13 at 18:21
-
In place of message post put your message. If it is of shorter length, use \n\n
\n\n\n\n as many times till you get your desired alerview. – Anoop Vaidya Jan 11 '13 at 18:23 -
What happened? no reply? and u accepted voted and again down voted? – Anoop Vaidya Jan 11 '13 at 18:42
-
Sorry, I didn't have time yesterday, but it was the solution, thanks! (and btw, I cannot vote up/down, so it wasn't me) – user1970792 Jan 12 '13 at 08:06
-
to vote up, you need 15 points... edit some questions and answers, you will get 2 points for each edit-post. and Now i am giving u +5. and when u cross 15 do vote :) – Anoop Vaidya Jan 12 '13 at 08:07
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.

- 1,202
- 3
- 13
- 34

- 2,302
- 1
- 16
- 35
-
This looks great, is perfect, but I need to have a pre-written message, but I might still use it in another situation. Thanks for taking the time and answering! – user1970792 Jan 11 '13 at 18:17
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:
Add a uialertview delegate to your view controller:
@interface ViewController : UIViewController <UIAlertViewDelegate>
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];
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 } }

- 1,407
- 1
- 17
- 27
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")
}))

- 33
- 6