UIAlertView与文本框和三个button问题在iOS 6

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; } } } } 

正如我所示的alertview textfield和三个button,它在ios 7中工作正常,但不是在iOS 6中。 看看这两个ios图像 – > 在这里输入图像说明

在这里输入图像说明

因为你都可以看到ios 6的alertview被打扰…但我没有得到我在做错的地方。

试试这个代码

  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)]; } } 

检查ios 6图像

在这里输入图像说明

编辑

对于旋转问题使用此代码

 - (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)]; } } 

我也在这里尝试过,看起来UIAlertView中的UIAlertViewStylePlainTextInput方式不能很好地处理3个或更多的button。

你的问题似乎直接与此有关:
iOS5:有人设法修复UIAlertView与三个button和文本字段(UIAlertViewStylePlainTextInput)?


UIAlertView的视图层次结构是不可取的(因为某些状态App Store被拒绝)
请参阅:如何在UIAlertView中移动button

您可以创build自己的自定义alertView:
http://iosdevtricks.blogspot.in/2013/04/creating-custom-alert-view-for-iphone.html

或使用预制的,如:
https://github.com/TomSwift/TSAlertView

如果你仍然想冒这个险,那就跟着Kalpesh在这里提出的build议(但是我不会推荐它)

而不是搞乱UIAlertView ,你应该使用自定义的(或模态)视图。 苹果也不喜欢你搞砸UIAlertView

所以,我的(也是最好的)对你的build议是用一个自定义的(或模态的)视图,在你的情况下也是一个正确的方法。

你做错了什么是你想要自定义UIAlertView并把子视图。 苹果一般不喜欢和禁止这种做法。 你可以尝试做的是以下几点:

  1. 您可以尝试像这样的自定义警报视图“复制”

  2. 写你自己的“复制”的警报视图,将符合你的需求,并在iOS6 / 7上的工作相同。