UIAlertView with textfield and three buttons issue

2019-01-24 20:19发布

问题:

     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Enter Student Name"        message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", @"Save and Add", nil];
                alert.tag = 1;
                alert.transform=CGAffineTransformMakeScale(1.0, 0.75);
                alert.alertViewStyle=UIAlertViewStylePlainTextInput;
                [alert show];

-(void)willPresentAlertView:(UIAlertView *)alertView {

    if (alertView.tag == 1) {


        for (UIView *view in alertView.subviews) {
            if ([view isKindOfClass:[UITextField class]]||
                [view isKindOfClass:[UIButton class]] || view.frame.size.height==31) {
                CGRect rect=view.frame;
                rect.origin.y += 65;
                view.frame = rect;
            }
        }

    }


}

As i am showing alertview with textfield and three buttons its working fine in ios 7 but not in ios 6 . have a look at both ios images -- >

as you all can see the alertview of ios 6 is disturbed ... but i am not getting what i am doing wrong there .

回答1:

Try this code

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Student Name"
                                                message:@""
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Save",@"Save & Add",nil];

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];



-(void)willPresentAlertView:(UIAlertView *)alertView {

if ([[[UIDevice currentDevice] systemVersion] floatValue] <7) 

   {
    [alertView setFrame:CGRectMake(17, 30, 286, 280)];
    NSArray *subviewArray = [alertView subviews];
    UILabel *message = (UILabel *)[subviewArray objectAtIndex:2];
    [message setFrame:CGRectMake(10, 46, 260, 20)];
    UIButton *cancelbutton = (UIButton *)[subviewArray objectAtIndex:3];
    [cancelbutton setFrame:CGRectMake(10, 125, 260, 42)];
    UIButton *savebutton = (UIButton *)[subviewArray objectAtIndex:4];
    [savebutton setFrame:CGRectMake(10, 170, 260, 42)];
    UIButton *saveAddbutton = (UIButton *)[subviewArray objectAtIndex:5];
    [saveAddbutton setFrame:CGRectMake(10, 220, 260, 42)];
    UITextField *textfield = (UITextField *)[subviewArray objectAtIndex:6];
    [textfield setFrame:CGRectMake(10, 80, 266, 50)];
    UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
    [placeTF setFrame:CGRectMake(15, 70, 256, 50)];
   }

}

Check for ios 6 image

EDIT

For rotation problem use this code

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (UIInterfaceOrientationPortrait == UIInterfaceOrientationIsPortrait(fromInterfaceOrientation))
{
           [alert setFrame:CGRectMake(100, 10, 286, 320)];
    NSArray *subviewArray = [alert subviews];
    UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2];
    [messageLB setFrame:CGRectMake(10, 46, 260, 20)];
    UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3];
    [cancelBT setFrame:CGRectMake(10, 125, 260, 42)];
    UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4];
    [okBT setFrame:CGRectMake(10, 170, 260, 42)];
    UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5];
    [searchBT setFrame:CGRectMake(10, 220, 260, 42)];
    UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6];
    [plateTF setFrame:CGRectMake(10, 80, 266, 50)];
    UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
    [placeTF setFrame:CGRectMake(15, 70, 256, 50)];


}
else{

    [alert setFrame:CGRectMake(17, 30, 286, 280)];
    NSArray *subviewArray = [alert subviews];
    UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2];
    [messageLB setFrame:CGRectMake(10, 46, 260, 20)];
    UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3];
    [cancelBT setFrame:CGRectMake(10, 125, 260, 42)];
    UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4];
    [okBT setFrame:CGRectMake(10, 170, 260, 42)];
    UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5];
    [searchBT setFrame:CGRectMake(10, 220, 260, 42)];
    UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6];
    [plateTF setFrame:CGRectMake(10, 80, 266, 50)];
    UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
    [placeTF setFrame:CGRectMake(15, 70, 256, 50)];

}
}


回答2:

I tried it here as well and it seems the style "UIAlertViewStylePlainTextInput" doesn't go well with 3 or more buttons in the UIAlertView.

your issue seems to be directly related to this:
iOS5: Has somebody managed to fix UIAlertView with three buttons and textfield (UIAlertViewStylePlainTextInput)?


It it won't be advisable to mess around with UIAlertView's view hierarchy (since some state App Store rejections)
refer: How to Move buttons down in UIAlertView

You can create your own custom alertView:
http://iosdevtricks.blogspot.in/2013/04/creating-custom-alert-view-for-iphone.html

or use a premade one, something like:
https://github.com/TomSwift/TSAlertView

if you still want to risk it, then go with what Kalpesh has suggested here (but i won't recommend it)



回答3:

In stead of messing with UIAlertView, you should use a custom presented (or a modal) view. Apple also doesn't like you to mess up with UIAlertView.

So, my (and also the best) suggestion for you is to go with a custom presented (or a modal) view which is also a correct approach in your case also.



回答4:

What you are doing wrong is you're trying to customize UIAlertView and put subviews into it. Apple generally dislikes and prohibits this approach. What you can try to do is the following:

  1. You can try custom alert-view 'replication' like this one

  2. Write your own 'replication' of alert-view that will fit your needs and work the same on both iOS6/7.