
シュミレーターではどうしてもiPhoneで動かすことが多いので、iPadでの不具合には気付きにくいですよね。
iPhoneでは普通にUIActivityViewが表示されていたのですが、iPadで試したところ、UIActivityViewが表示されるべきところでアプリが落ちてしまいました。
正しくiPadで正しくUIActivityViewが表示できるようにしたいです。
私がやったこと
1 2 3 4 5 6 7 8 9 10 |
//該当箇所 let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil) //iPadでActivityViewControllerを出すため activityVC.popoverPresentationController?.sourceView = self.view activityVC.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height / 2.0, width: 1.0, height: 1.0) |
どうやら
popoverPresentationController?.sourceView
popoverPresentationController?.sourceRect
この2つを指定する必要があるようです。
popoverPresentationControllerの詳細Summary
The nearest popover presentation controller that is managing the current view controller.
現在のView Controllerを管理している最も近いポップオーバープレゼンテーションコントローラー。
Declaration
var popoverPresentationController: UIPopoverPresentationController? { get }
Discussion
If the view controller or one of its ancestors is managed by a popover presentation controller, this property contains that object. This property is nil if the view controller is not managed by a popover presentation controller. If you created the view controller but have not yet presented it, accessing this property creates a popover presentation controller when the value in the modalPresentationStyle property is UIModalPresentationStyle.popover. If the modal presentation style is a different value, this property is nil.
View Controllerまたはその祖先の1つがポップオーバープレゼンテーションコントローラによって管理されている場合、このプロパティにはそのオブジェクトが含まれます。 View Controllerがポップオーバープレゼンテーションコントローラーによって管理されていない場合、このプロパティはnilです。 View Controllerを作成したがまだ表示していない場合、このプロパティにアクセスすると、modalPresentationStyleプロパティの値がUIModalPresentationStyle.popoverのときにpopoverプレゼンテーションコントローラが作成されます。モーダルプレゼンテーションスタイルが異なる値の場合、このプロパティはnilです。
sourceViewの詳細Summary
The view containing the anchor rectangle for the popover.
ポップオーバーのアンカー長方形を含むビュー。Declaration
var sourceView: UIView? { get set }
Discussion
Use this property in conjunction with the sourceRect property to specify the anchor location for the popover. Alternatively, you may specify the anchor location for the popover using the barButtonItem property.
このプロパティをsourceRectプロパティと組み合わせて使用して、ポップオーバーのアンカー位置を指定します。または、barButtonItemプロパティを使用して、ポップオーバーのアンカー位置を指定できます。
sourceRectの詳細Summary
The rectangle in the specified view in which to anchor the popover.
ポップオーバーを固定する、指定されたビュー内の長方形。Declaration
var sourceRect: CGRect { get set }
Discussion
Use this property in conjunction with the sourceView property to specify the anchor location for the popover. Alternatively, you may specify the anchor location for the popover using the barButtonItem property.
このプロパティをsourceViewプロパティと組み合わせて使用して、ポップオーバーのアンカー位置を指定します。または、barButtonItemプロパティを使用して、ポップオーバーのアンカー位置を指定できます。
sourceViewとsourceRectは組み合わせて使うことで、ポップオーバーのアンカー位置を指定でき、それを起点にUIActivityViewが表示されるようです。
シュミレータで見てみるとこのようになります。
CGRectで中心から出てくるようになっているため、このようになります。