How to move the buttons in a UIAlertView to make room for an inserted UITextField?

The simplest (and most proper way) to move the text view down is to add a message

[find setMessage:@"\n"];

Also, the reason 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.

Full example:

// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = @"Find";
// Add Buttons
[av addButtonWithTitle:@"Cancel"];
[av addButtonWithTitle:@"Find & Bring"];
[av addButtonWithTitle:@"Find & Go"];
[av addButtonWithTitle:@"Go to Next"];
// Make Space for Text View
av.message = @"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];

Note: You can also modify the subviews of UIAlertView, but since Apple has already changed the UIAlertView layout once you should check their class descriptions and frames against known values before setting new ones. You can even get something like this:

Preview
(source: booleanmagic.com)


Even if you can get this working it's not going to be very iPhone-y. The UIAlertView really is not designed for user input like this. If you look in all the Apple apps you'll see that they use a new view that displayed using the presentModalViewController: method of UIViewController.

Edit: This advice is no longer as true as it was when I wrote it. Apple have increasingly used alert views as text entry boxes and iOS5 even includes native support without having to mess around with views (check out the alertViewStyle property).

I think maybe if you need to have four buttons then using a custom UIViewController is probably still the right way to go. But if you just want to enter a password with OK/Cancel buttons then it's fine.


Zoul proposed the best method, to capture user input just do:

a) Add the UITextFieldDelegate protocol to your class.

b) Do something like

    UIAlertView *insertScore = [UIAlertView new];
    [insertScore setDelegate:self];
    [insertScore setTitle:@"New Title!"];
    [insertScore addButtonWithTitle:@"Cancel"];
    [insertScore addButtonWithTitle:@"Ok"];

    insertScore.message = @"\n";

    [insertScore addTextFieldWithValue:@"Input" label:@"player"];

    [[insertScore textField] setDelegate:self];

    [insertScore show]; 

    [insertScore release];

c) The crucial part was to set the delegate of the textField to self, then to access data you can simply:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
  NSLog(@"%@",[[alertView textField] text]);
}

Hope this helps someone, since I had to think a bit to get it right.

Tags:

Cocoa Touch