Today I will show how to transform an UIAlertView to a prompt of username and password textfields. Actually it’s quite easy for expert of adding subviews or creating own custom controls. So current code snippet is destined for begginers.
I propose to do it in next succession:
- Initilize standard UIAlertView. There is a trick here. We need to set a message body to @”nnn”, it makes a shift and create a place for the text fields.
- Create two UITextField and initialize them with specific frames. Then add them as subviews to the UIAlertView. Don’t forget to customize a background color, placeholders, securityText property.
- Lastly show UIAlertView with a little shift to top and set first UITextField as a FirstResponder if necessary.
The source code:
UITextField *textField; UITextField *textField2; UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Username and password" message:@"nnn" // IMPORTANT delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Enter", nil]; textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)]; [textField setBackgroundColor:[UIColor whiteColor]]; [textField setPlaceholder:@"username"]; [prompt addSubview:textField]; textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)]; [textField2 setBackgroundColor:[UIColor whiteColor]]; [textField2 setPlaceholder:@"password"]; [textField2 setSecureTextEntry:YES]; [prompt addSubview:textField2]; // set place [prompt setTransform:CGAffineTransformMakeTranslation(0.0, 110.0)]; [prompt show]; [prompt release]; // set cursor and show keyboard [textField becomeFirstResponder];

Pingback: Implement a UIAlertView with a UITextField « yimingcheung