IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView
2015-10-16 15:19
393 查看
苹果在IOS8版本上,新添加了一个UIAlertController用来代替 UIActionSheet 和 UIAlertView;
在工作中,会遇到修改这两个控件按钮颜色的要求,在网上一看,多是采用下边这种方法的:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
DDLogDebug(@"%@",actionSheet.subviews);
for (UIView *subview
in actionSheet.subviews) {
if ([subview
isKindOfClass:[UIButton
class]]) {
UIButton *button = (UIButton *)subview;
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateHighlighted];
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateNormal];
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateSelected];
}
}
}
但是在IOS8以后,无论是UIActionSheet、UIAlertView还是其他的一些控件的subviews就一直是空,不能返回任何UIView。所以在程序中根据系统版本采用不同的实现方式才是行之有效的,下面是UIAlertController的简单使用:
if ([[XWGlobalHelper
systemVersion] intValue] >
7.99) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction
actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];
UIAlertAction* fromPhotoAction = [UIAlertAction
actionWithTitle:@"从相册选择"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing =
NO;
imagePicker.delegate =
self;
[self
presentViewController:imagePicker
animated:YES
completion:nil];
}];
UIAlertAction* fromCameraAction = [UIAlertAction
actionWithTitle:@"相机"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.delegate =
self;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing =
NO;
imagePicker.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
[self
presentViewController:imagePicker
animated:YES
completion:nil];
}
}];
[alertController
addAction:cancelAction];
[alertController
addAction:fromCameraAction];
[alertController
addAction:fromPhotoAction];
[self
presentViewController:alertController
animated:YES
completion:nil];
} else {
UIActionSheet * actionSheet = [[UIActionSheet
alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"相机",@"从相册选择",
nil];
actionSheet.actionSheetStyle =
UIActionSheetStyleBlackOpaque;
actionSheet.delegate =
self;
[actionSheet
showInView:self.view];
}
上边的UIAlertControllerStyleActionSheet指定了显示效果为之前版本的UIActionSheet,另外还有一个UIAlertControllerStyleAlert指定了显示效果为之前版本的UIAlertView!
在工作中,会遇到修改这两个控件按钮颜色的要求,在网上一看,多是采用下边这种方法的:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
DDLogDebug(@"%@",actionSheet.subviews);
for (UIView *subview
in actionSheet.subviews) {
if ([subview
isKindOfClass:[UIButton
class]]) {
UIButton *button = (UIButton *)subview;
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateHighlighted];
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateNormal];
[button
setTitleColor:APP_TINT_COLOR
forState:UIControlStateSelected];
}
}
}
但是在IOS8以后,无论是UIActionSheet、UIAlertView还是其他的一些控件的subviews就一直是空,不能返回任何UIView。所以在程序中根据系统版本采用不同的实现方式才是行之有效的,下面是UIAlertController的简单使用:
if ([[XWGlobalHelper
systemVersion] intValue] >
7.99) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction
actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {}];
UIAlertAction* fromPhotoAction = [UIAlertAction
actionWithTitle:@"从相册选择"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing =
NO;
imagePicker.delegate =
self;
[self
presentViewController:imagePicker
animated:YES
completion:nil];
}];
UIAlertAction* fromCameraAction = [UIAlertAction
actionWithTitle:@"相机"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
if([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];
imagePicker.delegate =
self;
// imagePicker.allowsEditing = YES;
imagePicker.allowsEditing =
NO;
imagePicker.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
[self
presentViewController:imagePicker
animated:YES
completion:nil];
}
}];
[alertController
addAction:cancelAction];
[alertController
addAction:fromCameraAction];
[alertController
addAction:fromPhotoAction];
[self
presentViewController:alertController
animated:YES
completion:nil];
} else {
UIActionSheet * actionSheet = [[UIActionSheet
alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"相机",@"从相册选择",
nil];
actionSheet.actionSheetStyle =
UIActionSheetStyleBlackOpaque;
actionSheet.delegate =
self;
[actionSheet
showInView:self.view];
}
上边的UIAlertControllerStyleActionSheet指定了显示效果为之前版本的UIActionSheet,另外还有一个UIAlertControllerStyleAlert指定了显示效果为之前版本的UIAlertView!
相关文章推荐
- iOS开发UI篇—Quartz2D使用(绘制基本图形)
- JAVA学习3_Java多线程-工具篇-BlockingQueue
- iOS开发UI篇—Quartz2D(自定义UIImageView控件)
- iOS开发UI篇—核心动画(UIView封装动画)
- iOS开发UI篇—核心动画(转场动画和组动画)
- iOS开发UI篇—核心动画(关键帧动画)
- Android之MIUI系统BUG:调用拍照后不返回当前activity解决办法
- iOS开发UI篇—核心动画(基础动画)
- iOS开发UI篇—核心动画简介
- iOS开发UI篇—CAlayer(自定义layer)
- 设置 UISearchBar 输入字体颜色 和 默认字体颜色
- iOS开发UI篇—CAlayer层的属性
- 对于dequeueReuseableCellWithIdentifier的个人见解
- iOS开发UI篇—CAlayer(创建图层)
- NGUI的原理机制
- iOS开发UI篇—CALayer简介
- iOS开发UI篇—Quartz2D使用(截屏)
- iOS开发UI篇—Quartz2D使用(信纸条纹)
- ios学习之利用纯代码来创建uiimageVIew
- UIStackView入门