UIAlertView

Ok guys, here's my experiences with adding a UITextField to a UIAlertView and the nuances associated with it. All of this is done in code, so there's no interface builder to handle the layout, although I'm not sure that something better couldn't be accomplished using a custom view and XIB to display something that looks like an alert view.
 * http://www.iphonedevsdk.com/forum/iphone-sdk-development/1704-uitextfield-inside-uialertview.html

Alright, first things first. Adding a UITextField to a UIAlertView is as simple as instantiating the UITextField and adding it as a subview of your UIAlertView. I set the background color of the text field to be white so you can see where it is.

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil                          message:nil                          delegate:self                           cancelButtonTitle:@"Cancel"                           otherButtonTitles:nil];     UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];    [myAlertView addSubview:myTextField];    [myAlertView show];    [myAlertView release];

Here's a pic of the abboration that is your custom UIAlertView thus far.

After fiddling around with all the values for a while, I finally found the proper settings that allow the alert view to be displayed properly with the text field fully visible and functional. Since the UIAlertView isn't "made" to support this type of user interaction, you have to specify a message component for the alert view. This provides enough spacing so that you can insert the text field which will cover the message and thus keep things from over lapping. Here's the revised code and a picture of the properly formatted UIAlertView.

UIAlertView *myAlertView = UIAlertView alloc] initWithTitle:@"Your title here!"                          message:@"this gets covered"                           delegate:self                           cancelButtonTitle:@"Cancel"                           otherButtonTitles:@"OK",                           nil];    UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];    [myTextField setBackgroundColor:[UIColor whiteColor;    [myAlertView addSubview:testTextField];    [myAlertView show];    [myAlertView release];

After getting all that done, I ran into one more problem. When you touch the text field to enter text, the keyboard slides into view and covers part of the alert view.

To fix this, I created a translation transform and applied it to the alert view to move it higher up the screen so it wouldn't interfere with the keyboard. With that done, this is what it looks like. You still need to set up delegate methods for the alert view and the text field, but those are pretty simple and straight forward.

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0); [myAlertView setTransform:myTransform];

UIAlertView *alert; ... alert = [[[UIAlertView alloc] initWithTitle:@"Configuring Preferences\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [alert show]; UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; // Adjust the indicator so it is up a few pixels from the bottom of the alert indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50); [indicator startAnimating]; [alert addSubview:indicator]; [indicator release]; [alert dismissWithClickedButtonIndex:0 animated:YES];
 * http://www.idev101.com/code/User_Interface/UIAlertView.html
 * http://iphonedevelopertips.com/user-interface/uialertview-without-buttons-please-wait-dialog.html