67

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

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

4

8 回答 8

55

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

(从这个 StackOverflow 线程复制。)

自定义 iOS7 AlertView 对话框

于 2013-09-23T11:15:32.767 回答
35

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

  1. 截取 Apple 警报的屏幕截图以供参考(字体大小、间距、宽度)
  2. 为按钮创建一个带有标题、消息、自定义视图和表格的 xib(Apple 使用表格而不是UIButton现在,默认表格单元格就足够了)。请注意,您需要 3 个按钮表:两个用于左右按钮(只要按钮数量为 2),另一个用于其他情况(一个按钮或超过 2 个按钮)。
  3. 实现UIAlertView自定义警报中的所有方法。

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

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

编辑:

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

视差效果:

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

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

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

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
                     }];
于 2013-09-14T18:55:38.400 回答
32

我编写了一个完整的 UIAlertView 实现,它模仿了完整的 UIAlertView API,但添加了我们长期以来都想要的 contentView 属性:SDCAlertView

图片
(来源:github.io

于 2013-11-24T17:07:38.083 回答
27

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

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

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

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

希望它可以帮助一些人,谢谢:)

于 2014-04-04T11:38:54.870 回答
17

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];
于 2013-10-03T20:27:50.863 回答
10

UIAlertViewiOS7 中不会有自定义视图, contentViewApple 也不会改变主意,所以addSubview现在在UIAlertView.

根据 Apple 论坛中的许多帖子,一个不错的选择是SVProgressHUD 。

编辑

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

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

其他不错的选择:

ios-custom-alertview by wimagguc

MZFormSheetController

于 2013-09-13T15:40:20.910 回答
1

您可以在此处找到无需额外课程的简单解决方案

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

于 2014-03-11T11:57:01.240 回答
1

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

为什么它很酷:

  • 支持自定义视图
  • 支持iOS7
  • 它是视图控制器
  • 它的行为和外观类似于本机警报视图,包括运动效果
  • 可定制
  • 类似 UIAlertController 的界面
于 2015-08-31T18:38:10.363 回答