UIAlertView
在 iOS7 中不推荐使用添加一些控件的addSubview
方法。据我所知,苹果承诺增加contentView
财产。
iOS 7现在发布了,我看到这个属性没有添加。这就是为什么我搜索一些能够向此警报视图添加进度条的自定义解决方案。例如类似于TSAlertView的东西,但更适合在iOS 7中使用。
UIAlertView
在 iOS7 中不推荐使用添加一些控件的addSubview
方法。据我所知,苹果承诺增加contentView
财产。
iOS 7现在发布了,我看到这个属性没有添加。这就是为什么我搜索一些能够向此警报视图添加进度条的自定义解决方案。例如类似于TSAlertView的东西,但更适合在iOS 7中使用。
我只花了 1 天时间就创建了自己的警报视图,看起来与 Apple 的完全一样
UIButton
现在,默认表格单元格就足够了)。请注意,您需要 3 个按钮表:两个用于左右按钮(只要按钮数量为 2),另一个用于其他情况(一个按钮或超过 2 个按钮)。实现UIAlertView
自定义警报中的所有方法。
Show/Dismiss - 你可以为你的警报创建一个特定的模式窗口,但我只是把我的警报放在我的根视图控制器的顶部。将可见警报注册到静态数组。如果显示第一个警报/关闭最后一个警报,请将窗口/视图控制器的色调模式更改为暗淡/自动并添加/删除暗淡视图(黑色,alpha = 0.2)。
编辑:
模糊背景和视差效果示例代码都可以在“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
}];
我编写了一个完整的 UIAlertView 实现,它模仿了完整的 UIAlertView API,但添加了我们长期以来都想要的 contentView 属性:SDCAlertView。
(来源:github.io)
对于那些喜欢简单有效的方法而无需编写代码的人。这是一个很酷的解决方案,无需使用任何其他私有框架将子视图添加到 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];
}
希望它可以帮助一些人,谢谢:)
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];
UIAlertView
iOS7 中不会有自定义视图, contentView
Apple 也不会改变主意,所以addSubview
现在在UIAlertView
.
根据 Apple 论坛中的许多帖子,一个不错的选择是SVProgressHUD 。
编辑:
在iOS7中正式没有addSubview
也没有子类化。UIAlertView
UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
其他不错的选择:
您可以在此处找到无需额外课程的简单解决方案
它基于为普通UIAlertView设置附件视图。
PKAlertController ( https://github.com/goodpatch/PKAlertController ) 是一个很棒的库。我测试了很多类似的库,这满足了我的所有要求。
为什么它很酷: