我知道现在回答为时已晚,但如果您已经对此感兴趣,这可能对您有用:
在 .h 文件中,给出这个代表:
@interface ViewController : UIViewController <UIPopoverControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
将弹出框创建为属性:
@property (nonatomic, strong) UIPopoverController *popoverController;
最后在 .m 文件中:
-(IBAction)showPhotoLibray:(id)sender
{
BOOL hasGellery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGellery ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.theViewObject frame]
fromView:[self.theViewObject superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
self.theViewObject 是调用该方法的对象视图控制器的出口,例如 UIButton