I managed to customize the UIAlertView by inserting two UITextFields as subzones inside it, but somehow the keyboard broke.
When I try to fire him, the lagging first defendant, he does nothing, he just stays there.
Here's my code, well, only the relevant parts (this is what I nested in a subclass of UIAlertView):
#pragma mark - #pragma mark UIView - (void)layoutSubviews { [super layoutSubviews]; NSArray *subviews = [self subviews]; UIView *backgroundView = [subviews objectAtIndex:0]; CGRect firstFrame = [[subviews objectAtIndex:2] frame]; CGRect backgroundFrame = [backgroundView frame]; CGFloat displacement = kTextHeight * 2 + kPadding * 3; CGFloat pivot = firstFrame.origin.y + firstFrame.size.height; CGRect nameFrame = CGRectMake(firstFrame.origin.x, pivot + kPadding, kTextWidth, kTextHeight); CGRect descriptionFrame = CGRectMake(firstFrame.origin.x, pivot + kTextHeight + kPadding * 2, kTextWidth, kTextHeight); if (_nameField == nil && _descriptionField == nil) { // first UITextField _nameField = [[UITextField alloc] initWithFrame:CGRectZero]; [_nameField setPlaceholder:kNamePlaceholder]; [_nameField setBorderStyle:UITextBorderStyleRoundedRect]; [_nameField setDelegate:self]; // second UITextField _descriptionField = [[UITextField alloc] initWithFrame:CGRectZero]; [_descriptionField setPlaceholder:kDescriptionPlaceholder]; [_descriptionField setBorderStyle:UITextBorderStyleRoundedRect]; [_descriptionField setDelegate:self]; [self addSubview:_nameField]; [self addSubview:_descriptionField]; } // set the new frames to each text field [_nameField setFrame:nameFrame]; [_descriptionField setFrame:descriptionFrame]; // increment background image-view height by "displacement" backgroundFrame.size.height += displacement; [backgroundView setFrame:backgroundFrame]; // displace by "diplacement" every subview positioned after "pivot" for (UIView *view in subviews) { CGRect viewRect = [view frame]; if (viewRect.origin.y > pivot) [view setFrame:CGRectOffset(viewRect, 0., displacement)]; } } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return YES; } #pragma mark - #pragma mark <UITextFieldDelegate> - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; }
Here is my code in one big snippet:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> static CGFloat kTextWidth = 260.; static CGFloat kTextHeight = 25.; static CGFloat kPadding = 10.; static NSString *kNamePlaceHolder = @"Name"; static NSString *kDescriptionPlaceHolder = @"Description"; // interfaces @interface AppDelegate: NSObject <UIApplicationDelegate> { UIWindow *_window; } - (void)showAlertView; @end @interface AlertView: UIAlertView <UITextFieldDelegate> { UITextField *_nameField; UITextField *_descriptionField; } @end // implementations @implementation AppDelegate #pragma mark - #pragma mark NSObject - (void)dealloc { [_window release]; [super dealloc]; } #pragma mark - #pragma mark AppDelegate - (void)showAlertView { AlertView *alertView = [[[AlertView alloc] initWithTitle:@"Title" message:@"Body" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease]; [alertView show]; } #pragma mark - #pragma mark <UIApplicationDelegate> - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options { _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [_window setBackgroundColor:[UIColor whiteColor]]; [_window makeKeyAndVisible]; [NSTimer scheduledTimerWithTimeInterval:3. target:self selector:@selector(showAlertView) userInfo:nil repeats:NO]; return YES; } @end @implementation AlertView #pragma mark - #pragma mark NSObject - (void)dealloc { [_nameField setDelegate:nil]; [_descriptionField setDelegate:nil]; [_nameField release]; [_descriptionField release]; [super dealloc]; } #pragma mark - #pragma mark UIView - (void)layoutSubviews { [super layoutSubviews]; NSArray *subviews = [self subviews]; UIView *backgroundView = [subviews objectAtIndex:0]; CGRect firstFrame = [[subviews objectAtIndex:2] frame]; CGRect backgroundFrame = [backgroundView frame]; CGFloat displacement = kTextHeight * 2 + kPadding * 3; CGFloat pivot = firstFrame.origin.y + firstFrame.size.height; CGRect nameFrame = CGRectMake(firstFrame.origin.x, pivot + kPadding, kTextWidth, kTextHeight); CGRect descriptionFrame = CGRectMake(firstFrame.origin.x, pivot + kTextHeight + kPadding * 2, kTextWidth, kTextHeight); if (_nameField == nil && _descriptionField == nil) { // first UITextField _nameField = [[UITextField alloc] initWithFrame:CGRectZero]; [_nameField setPlaceholder:kNamePlaceholder]; [_nameField setBorderStyle:UITextBorderStyleRoundedRect]; [_nameField setDelegate:self]; // second UITextField _descriptionField = [[UITextField alloc] initWithFrame:CGRectZero]; [_descriptionField setPlaceholder:kDescriptionPlaceholder]; [_descriptionField setBorderStyle:UITextBorderStyleRoundedRect]; [_descriptionField setDelegate:self]; [self addSubview:_nameField]; [self addSubview:_descriptionField]; } // set the new frames to each text field [_nameField setFrame:nameFrame]; [_descriptionField setFrame:descriptionFrame]; // increment background image-view height by "displacement" backgroundFrame.size.height += displacement; [backgroundView setFrame:backgroundFrame]; // displace by "diplacement" every subview positioned after "pivot" for (UIView *view in subviews) { CGRect viewRect = [view frame]; if (viewRect.origin.y > pivot) [view setFrame:CGRectOffset(viewRect, 0., displacement)]; } } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return YES; } #pragma mark - #pragma mark <UITextFieldDelegate> - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } @end int main(int argc, char **argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); [pool drain]; return retVal; }
ios objective-c uitextfield keyboard first-responder
Jean-pierre chauvel
source share