UIActionSheet和UIAlertController在iPhone和iPad上的那些事儿。
整理一下今天的所得。应该从哪里说起呢?
从今天用pad测试工程的时候发现工程中拍照上传没法用了。我用iphone可以用呀。一开始以为是系统的问题,升级了ios10之后不是有对相册,照相机等东西的私有设置吗,设置了白名单之后还是不可以。然后我就用断点打,发现是
UIActionSheet *myActionSheet = [[UIActionSheet alloc]initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"打开照相机",@"从手机相册获取",nil];
[myActionSheet showInView:[UIApplication sharedApplication].keyWindow];
这段代码的问题
查了一下UIActionSheet再iPhone 和iPad上的显示是不同的。所以就改成了
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"打开照相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self takePhoto];
}];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"从手机相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self LocalPhoto];
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
alertController.popoverPresentationController.sourceView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
alertController.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
这些才终于解决了。我们用的库是ZLPhotoLib,这是一个14年beiqing写的库。
注意哈:alertController.popoverPresentationController.sourceView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
alertController.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);这两句要是不指定可是会崩溃的这是UIAlertController的坑。