【问题标题】:UIAlertView with textfield and three buttons issue in ios 6ios 6中带有文本字段和三个按钮的UIAlertView问题
【发布时间】:2013-10-22 09:09:16
【问题描述】:
     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;
            }
        }

    }


}

当我显示带有文本字段和三个按钮的警报视图时,它在 ios 7 中工作正常,但在 ios 6 中却没有。 看看两个 ios 图片 -->

你们都可以看到 ios 6 的警报视图受到干扰......但我没有明白我在那里做错了什么。

【问题讨论】:

  • 如果您在 iOS7 中修改 UIAlertView 的私有视图层次结构,您的应用将被商店拒绝。 iOS7 和 iOS6 之间的 UIAlertView 上的视图层次结构很可能是不同的。
  • 您确定我的应用在使用此代码后会被拒绝
  • @kshitijgodara 检查我的答案。它完美地工作。我已经在 ios6 和 ios 7 上测试过
  • 检查 UIAlertView 类参考 - developer.apple.com/library/ios/documentation/uikit/reference/… 它声明“此类的视图层次结构是私有的,不得修改”。用你喜欢的方式解释。

标签: ios ios6 ios7 uialertview


【解决方案1】:

试试这个代码

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

}
}

【讨论】:

  • 您的代码运行良好,但如果我此时旋转我的单元格,它确实会受到干扰。
【解决方案2】:

我也在这里尝试过,似乎 UIAlertView 中的 3 个或更多按钮的样式“UIAlertViewStylePlainTextInput”不适合。

您的问题似乎与此直接相关:
iOS5: Has somebody managed to fix UIAlertView with three buttons and textfield (UIAlertViewStylePlainTextInput)?


不建议弄乱 UIAlertView 的视图层次结构(因为某些状态的 App Store 拒绝)
参考:如何Move buttons down in UIAlertView

您可以创建自己的自定义警报视图:
http://iosdevtricks.blogspot.in/2013/04/creating-custom-alert-view-for-iphone.html

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

如果你仍然想冒险,那么按照 Kalpesh 在这里的建议(但我不会推荐)

【讨论】:

    【解决方案3】:

    不要乱用UIAlertView,您应该使用自定义呈现(或模态)视图。 Apple 也不喜欢你搞砸UIAlertView

    所以,我(也是最好的)对您的建议是使用自定义呈现(或模态)视图,这在您的情况下也是正确的方法。

    【讨论】:

      【解决方案4】:

      您做错了什么是您尝试自定义 UIAlertView 并将子视图放入其中。 苹果通常不喜欢并禁止这种方法。 您可以尝试执行以下操作:

      1. 您可以尝试自定义警报视图“复制”like this one

      2. 编写您自己的警报视图“复制”,以满足您的需求并在 iOS6/7 上同样工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多