ios iOS7 中的 UIAlertView addSubview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18729220/
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:04:12  来源:igfitidea点击:

UIAlertView addSubview in iOS7

iosobjective-cuialertviewios7

提问by B.S.

Adding some controls to UIAlertViewwas deprecated in iOS7 using addSubviewmethod. As I know Apple promised to add contentViewproperty.

UIAlertView在 iOS7 中使用addSubview方法添加一些控件已被弃用。据我所知,苹果承诺增加contentView财产。

iOS 7is released now and I see that this property is not added. That is why I search for some custom solution with ability to add progress bar to this alertView. Something for example similar to TSAlertView, but more ready for using in iOS 7.

iOS 7现已发布,我看到未添加此属性。这就是为什么我搜索一些能够向这个 alertView 添加进度条的自定义解决方案。例如类似于TSAlertView 的东西,但更适合在iOS 7 中使用

回答by Wimagguc

Here is a project on Githubto add any UIView to an UIAlertView-looking dialog on iOS7.

这是 Github上的一个项目,用于将任何 UIView 添加到 iOS7 上的 UIAlertView 外观对话框。

(Copied from this StackOverflow thread.)

(从这个 StackOverflow 线程复制。)

Custom iOS7 AlertView dialog

Custom iOS7 AlertView dialog

回答by Sulthan

It took me only 1 day to create my own alert view that looks exactly like Apple's

我只用了 1 天就创建了自己的警报视图,它看起来与 Apple 的完全一样

  1. Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
  2. Create a xib with title, message, custom view and tables for buttons (Apple uses tables instead of UIButtonnow, default table cell is good enough). Note you need 3 button tables: two for left and right buttons (whenever the number of buttons is 2), another one for the other cases (one button or more than 2 buttons).
  3. Implement all the methods from UIAlertViewon your custom alert.

  4. Show/Dismiss - you can create a specific modal window for your alerts but I just put my alerts on top of my root view controller. Register your visible alerts to a static array. If showing the first alert/dismissing the last, change tint mode of your window/view controller to dimmed/to automatic and add/remove a dimming view (black with alpha = 0.2).

  5. Blurred background - use Apple's sample code (I used opaque white)
  6. 3D dynamic effects - use Apple's sample code (5 lines of code). If you want a nice effect, take a slightly bigger snapshot in step 5 and add inverse animators for alert background and foreground.
  1. 截取Apple的alert截图以供参考(字体大小、间距、宽度)
  2. 创建一个带有标题、消息、自定义视图和表格的 xib(Apple 使用表格而不是UIButton现在,默认表格单元格就足够了)。请注意,您需要 3 个按钮表:两个用于左右按钮(当按钮数为 2 时),另一个用于其他情况(一个按钮或两个以上按钮)。
  3. 实现UIAlertView自定义警报中的所有方法。

  4. 显示/关闭 - 你可以为你的警报创建一个特定的模式窗口,但我只是把我的警报放在我的根视图控制器之上。将可见警报注册到静态数组。如果显示第一个警报/解除最后一个警报,请将窗口/视图控制器的色调模式更改为变暗/自动并添加/删除变暗视图(黑色,alpha = 0.2)。

  5. 背景模糊 - 使用 Apple 的示例代码(我使用了不透明的白色)
  6. 3D 动态效果 - 使用 Apple 的示例代码(5 行代码)。如果你想要一个好的效果,在步骤 5 中拍摄一个稍微大一点的快照,并为警报背景和前景添加反向动画。

EDIT:

编辑:

Both blurred background and the paralax effect sample code can be found in "iOS_RunningWithASnap" WWDC 2013 sample code

模糊背景和视差效果示例代码可以在“iOS_RunningWithASnap”WWDC 2013 示例代码中找到

Paralax effect:

视差效果:

UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                                                     type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];
xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];

UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                                                     type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];
yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];

UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];
group.motionEffects = @[xAxis, yAxis];
[self addMotionEffect:group];

The blurred background is the only complicated thing. If you can use an opaque color instead, use it. Otherwise it's a lot of experimenting. Also note that blurred background is not a good solution when the background is dark.

模糊的背景是唯一复杂的东西。如果您可以改用不透明颜色,请使用它。否则,这是很多实验。另请注意,当背景较暗时,模糊背景不是一个好的解决方案。

For the show/dismiss animationg, I am using the new spring animation method:

对于显示/关闭动画,我使用的是新的弹簧动画方法:

void (^animations)() = ^{
    self.alpha = 1.0f;
    self.transform = CGAffineTransformIdentity;
};

self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);

[UIView animateWithDuration:0.3
                      delay:0.0
     usingSpringWithDamping:0.7f
      initialSpringVelocity:0.0f
                    options:UIViewAnimationOptionCurveLinear
                 animations:animations
                 completion:^(BOOL completed) {
                         //calling UIAlertViewDelegate method
                     }];

回答by Scott Berrevoets

I wrote a full implementation of UIAlertView that mimics the complete UIAlertView API, but adds the contentView property we've all wanted for so long: SDCAlertView.

我写了一个完整的 UIAlertView 实现,它模仿了完整的 UIAlertView API,但添加了我们一直想要的 contentView 属性:SDCAlertView

image

image

回答by Eshwar Chaitanya

For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.

对于那些喜欢简单有效的方法而不必编写代码行的人。这是一个很酷的解决方案,不使用任何其他私有框架作品将子视图添加到 ios 7 警报视图,即

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

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

Sample code for better understanding,

示例代码以便更好地理解,

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:@"image.png"];
[imageView setImage:wonImage];

//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
      [alertView setValue:imageView forKey:@"accessoryView"];
}else{
      [alertView addSubview:imageView];
}

Hope it helps some one,thanks :)

希望对大家有所帮助,谢谢:)

回答by Ganesh manoj

For IOS7

IOS7

UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"Enter Form Name" 
                                               message:@""
                                               delegate:self 
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"Ok", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;

[alertView1 show];

回答by Tarek Hallak

There wont be UIAlertViewwith custom views in iOS7, nor contentViewwhich Apple changed its mind about, so addSubviewis impossible now in UIAlertView.

UIAlertView在 iOS7 中不会有自定义视图contentView,苹果也不会改变主意,所以addSubview现在在UIAlertView.

A good alternative will be SVProgressHUD, according to many threads in Apple's forum.

根据苹果论坛中的许多帖子,一个很好的选择是SVProgressHUD

Edit:

编辑

There is officially no addSubviewnor subclassing for UIAlertViewin iOS7.

iOS7 中正式没有addSubview也没有子类UIAlertView

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.

UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

Other good alternatives:

其他不错的选择:

ios-custom-alertview by wimagguc

ios-custom-alertview 由 wimagguc

MZFormSheetController.

MZFormSheetController

回答by Igor Palaguta

PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.

PKAlertController ( https://github.com/goodpatch/PKAlertController) 是一个很棒的库。我测试了很多类似的库,这满足了我的所有要求。

Why it is cool:

为什么很酷:

  • Supports custom view
  • Supports iOS7
  • It is view controller
  • It behaves and looks like native alert view, including motion effects
  • Customizable
  • Similar interface like in UIAlertController
  • 支持自定义视图
  • 支持iOS7
  • 它是视图控制器
  • 它的行为和外观类似于原生警报视图,包括运动效果
  • 可定制
  • 类似 UIAlertController 中的界面

回答by malex

You can find simple solution without extra classes here

您可以在这里找到没有额外课程的简单解决方案

It is based on setting accessoryViewfor ordinary UIAlertView.

它基于为普通UIAlertView设置附件视图