【问题标题】:How to add subview inside UIAlertView for iOS 7?如何在 iOS 7 的 UIAlertView 中添加子视图?
【发布时间】:2013-09-16 12:59:18
【问题描述】:

我在 iTunes 商店中有一个应用程序,它在 UIAlertView 上显示一些 UILabelUIWebView。根据会话视频,addSubView for UIAlertView 将不起作用。他们谈到了ContentView。但在 GM Seeds SDK 中,我找不到该属性,似乎没有其他办法。

我唯一能做的就是创建一个UIView 的自定义子类,让它像UIAertView 一样工作。你能推荐任何其他简单的解决方案吗?

感谢您的帮助。

【问题讨论】:

标签: ios objective-c ios7 uialertview


【解决方案1】:

你真的可以在 iOS7 中将 accessoryView 更改为 customContentView(在 iOS8 中似乎也是如此)UIAlertView

[alertView setValue:customContentView forKey:@"accessoryView"];

试试这个代码:

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[av setValue:v forKey:@"accessoryView"];
v.backgroundColor = [UIColor yellowColor];
[av show];

请记住,您需要在调用之前设置自定义 accessoryView [alertView show]

【讨论】:

  • 检查 IOS 版本是否向后兼容,否则 [av setValue:v forKey:@"accessoryView"];会使应用程序崩溃
  • 这个问题和答案仅适用于 iOS 7。但无论如何感谢您的评论。
  • @DarthMike 我认为只有在某些拒绝后才投票更诚实......
  • 我在生产应用程序中使用它。但是,在 iOS8 中,这不再有效。
  • 嗯,它在 iOS 8 中工作,但它并没有使您的视图居中。你必须自己做。是的,正如 Eiko 所说,如果您尝试通过调用 valueForKey: 来获取视图,它会崩溃。但是,例如,如果您在视图中有一个进度条,您可以为其设置一个全局变量,它会正常工作。
【解决方案2】:

从 iOS 7 开始,AddSubview 是不可能的。

唯一的方法是创建一个自定义的 UIView 子类,它可以充当 UIAlertView。我可以在 Github 上找到一些组件。

链接的Custom Alert 似乎运行良好。通过进行适当的版本检查,我们可以确定是使用原生 UIAlertView 还是 CustomAlertView。

【讨论】:

    【解决方案3】:

    UIAlertView 添加子视图在iOS7 中与早期iOS 版本不同。试试这样的:

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [myAlertView setValue:myCustomView forKey:@"accessoryView"]; //works only in iOS7
    } else {
        myAlertView.message = @"\n"; //or just add \n to the end of the message (it's a workaround to create space inside the alert view
        [myAlertView addSubview:myCustomView];
    }
    
    [myAlertView show]; //show the alert AFTER CUSTOMIZATION  
    

    【讨论】:

      【解决方案4】:

      相信如果你使用它你也会得到帮助:)

      syncProgressBarView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
      syncProgressBarView.frame =CGRectMake(20, 55, 250, 20);
      [syncProgressBarView setProgress:0.0f animated:NO];
      syncProgressBarView.backgroundColor =[UIColor clearColor];
      [progressView addSubview:syncProgressBarView];
      

      为子查看创建新视图UIProgressView

      progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 100)];
      [[UIApplication sharedApplication].keyWindow addSubview:progressView];
      progressView.backgroundColor = [UIColor clearColor];
      progressView.center = [UIApplication sharedApplication].keyWindow.center;
      syncProgressBarView.frame = CGRectMake(progressView.frame.size.width/2 - 250/2, progressView.frame.size.height/2 - 10, 250, 20);
      [[UIApplication sharedApplication].keyWindow bringSubviewToFront:[progressView superview]];
      

      还有一件你必须做的事情......

      dispatch_async(dispatch_get_main_queue(), ^{
                      [self updateProgressBar];
                  });
      

      【讨论】:

        【解决方案5】:

        已接受答案中链接的 github 上的自定义警报视图很棒。但是,您不能直接从 viewdidload 方法启动它。封闭的 UIView 附加到应用程序的第一个窗口,因此您必须等待片刻。我将此代码添加到 viewdidload 中,我发现这是一个已解决的问题。

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
              // Open the dialog here
           });
        

        【讨论】:

          【解决方案6】:

          一种解决方案是继承 UIAlertView 并检测“_UIModalItemAlertContentView”视图。 我的 AlertView 有一个 UITextField,所以我用它来检测内容视图:

          - (void)show
          {
              [ super show ];
              UIView *contentView=nil;
              if([[[UIDevice currentDevice] systemVersion] floatValue ] < 7.0f)
              {
                  contentView=self;
              } else {
                  UIView *rootView=[self textFieldAtIndex:0];
                  while((rootView=[rootView superview])!=nil)
                  {
                      if([ NSStringFromClass([rootView class]) isEqualToString:@"_UIModalItemAlertContentView"])
                      {
                          contentView=rootView;
                          break;
                      }
                  }
              }
          
              if(contentView!=nil)
              {
                  [ contentView addSubview: ... ];
              }
          }
          

          【讨论】:

          • NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO 你永远不应该继承UIAlertView 参见"Subclassing notes" (developer.apple.com/library/ios/documentation/uikit/reference/…) - "The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified." 部分,这会让你的应用被应用商店拒绝 -1
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多