如果只是手机使用,按照正常的操作就可以了,但是今天在pad上试了一把,结果奔溃了,为啥呢,打印结果大概意思就是,弹出来的那个框需要挂靠在某个视图上,直接上代码吧
这是一段选择头像的提示框:
- (void)editHeader:(UIButton *)btn{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"设置头像" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self openImagePicker:UIImagePickerControllerSourceTypeCamera];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
[self openImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
if([DeviceInfo deviceIsPhone]){
[self presentViewController:alert animated:YES completion:nil];
}else{
UIPopoverPresentationController *popPresenter = [alert
popoverPresentationController];
popPresenter.sourceView = btn;
popPresenter.sourceRect = btn.bounds;
[self presentViewController:alert animated:YES completion:nil];
}
}
判断设备的类
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DeviceInfo : NSObject
+ (BOOL)deviceIsPhone;
@end
.m
#import "DeviceInfo.h"
@implementation DeviceInfo
+ (BOOL)deviceIsPhone{
BOOL _isIdiomPhone = YES;
UIDevice *currentDevice = [UIDevice currentDevice];
if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
_isIdiomPhone = YES;
}
else if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad){
_isIdiomPhone = NO;
}
return _isIdiomPhone;
}
@end