如何在 iOS 7 的 UIAlertView 中添加子视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18759206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:08:52  来源:igfitidea点击:

How to add subview inside UIAlertView for iOS 7?

iosobjective-cios7uialertview

提问by Apurv

I am having an application on iTunes store which displays some UILabeland UIWebViewon UIAlertView. According to session video, addSubViewfor UIAlertViewwill not work. They have talked about ContentView. But in the GM Seeds SDK, I could not find that property and there seems to be no other way.

我有在iTunes商店它显示了一些应用程序UILabelUIWebViewUIAlertView。根据会话视频,addSubViewforUIAlertView将不起作用。他们谈过ContentView。但是在 GM Seeds SDK 中,我找不到该属性,似乎没有其他方法。

The only thing I can do is to create a custom subclass of UIViewand make it work like UIAertView. Can you suggest any other simple solution?

我唯一能做的就是创建一个的自定义子类UIView并使其像UIAertView. 你能提出任何其他简单的解决方案吗?

Thanks for the help.

谢谢您的帮助。

回答by malex

You can really change accessoryViewto customContentViewin iOS7 (and it seems that in iOS8 as well) UIAlertView

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

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

Try this code:

试试这个代码:

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

Remember that you need set custom accessoryViewbefore the call [alertView show]

请记住,你需要一套定制accessoryView的呼叫前[alertView秀]

enter image description here

在此处输入图片说明

回答by Apurv

AddSubview is not possible from iOS 7.

AddSubview 在 iOS 7 中是不可能的。

The only way is to create a custom UIView subclass which can act as UIAlertView. I could find out few components on Github.

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

The linked one Custom Alertseems to work well. By putting proper version check one can identify to go for native UIAlertView or CustomAlertView.

链接的一个自定义警报似乎运行良好。通过进行适当的版本检查,您可以确定使用本机 UIAlertView 或 CustomAlertView。

回答by Andra Todorescu

Adding a subview to an UIAlertViewis different in iOS7 from earlier iOS versions. Try something like this:

UIAlertView在 iOS7 中添加子视图与早期的 iO​​S 版本不同。尝试这样的事情:

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  

回答by Emon

Think you will get help also if you use it :)

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

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

Making a new view for sub-viewing UIProgressView

为子视图创建新视图 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]];

One more thing you have to do...

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

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

回答by leenyburger

The custom alert view on github linked in the accepted answer is great. However, you cannot launch this directly from the viewdidload method. The enclosing UIView is attached to the app's first window, so you have to wait a split second. I added this code to viewdidload which I found as a closed issue.

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

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

回答by Guizmo

A solution is to subclass UIAlertView and detect the "_UIModalItemAlertContentView" view. My AlertView had a UITextField, so I used it to detect the content view :

一个解决方案是继承 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: ... ];
    }
}