首页 文章

iPad上的ActionSheet崩溃(相机按钮)

提问于
浏览
0

我的ActionSheet遇到了一些麻烦 . 在iPhone上它工作正常,但在iPad上它只是崩溃 .

我有一个显示相机的按钮,但我无法在iPad上正确显示 .

码:

class pictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate {
    @IBOutlet weak var picture: UIImageView!
    override func viewDidLoad() {
    }

    @IBAction func choosePictureAction(sender: AnyObject) {
        let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
        presentViewController(alertPictureFrom, animated: true, completion: nil)
        alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in
            let picker = UIImagePickerController()
            picker.sourceType = .Camera
            picker.delegate = self
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))

        alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in
            let picker = UIImagePickerController()
            picker.sourceType = .PhotoLibrary
            picker.delegate = self
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        picture.image = image
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

}

您的应用程序提供了样式UIAlertControllerStyleActionSheet的UIAlertController() . 具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover . 您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息 . 您必须提供sourceView和sourceRect或barButtonItem . 如果在显示警报控制器时未知此信息,则可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供该信息 .

3 回答

  • 3

    正如错误消息所示:"You must provide either a sourceView and sourceRect or a barButtonItem"通过 popoverPresentationController

    这将允许UIKit知道弹出窗口的位置,然后您可以获得弹出窗口附加到指定元素的可爱行为 .

    例如:

    alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
    alertPictureFrom.popoverPresentationController?.sourceView = self.view
    

    现在该示例完全用于演示目的,您需要找出弹出窗口的 sourceViewsourceRect .

    参考文件:

  • 0

    尝试(在UIButton的情况下):

    picker.popoverPresentationController?.sourceView = testButton
    picker.popoverPresentationController?.sourceRect = testButton.bounds
    picker.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Right.bounds
    
  • 0

    您的错误Genrate由于UIActionSheet

    UIActionSheet不支持ipad . 对于iphone没什么关联的UIActionsheet工作正常,IPAD UIActionSheet在另一个视图中打开就好了

    UIActionSheetObjects.showFromRect(CGRectMake(150, 0, 300,200 ) ,inView:self.view, animated:YES)
    

    它会对你有用......

相关问题