我正在创建一个共享表,其中有人可以从文本字段共享文本。我已经在iPhone上工作,但应用程序在iPad上崩溃了。我知道它必须是一个popover形式,并且在Objective-C的那一天回到了这个问题,但我似乎无法在Swift上找到它。这是我的共享表中的代码:
@IBAction func myShareButton(sender: AnyObject) {
// Hide the keyboard
textView.resignFirstResponder()
// Check and see if the text field is empty
if (textView.text == "") {
// The text field is empty so display an Alert
displayAlert("Warning", message: "You haven't written anything yet!")
} else {
// We have contents so display the share sheet
displayShareSheet(textView.text!)
}
}
// Show Warning
func displayAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alertController, animated: true, completion: nil)
return
}
// Display Share Sheet
func displayShareSheet(shareContent:String) {
let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: {})
}
答案 0 :(得分:3)
我今天也实施了股票表,并且遇到了完全相同的问题。你需要在演示之前添加它。
if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: CGRectGetMidX(view.bounds), y: CGRectGetMidY(view.bounds), width: 0, height: 0)
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0) //Removes arrow as I dont want it
}
第1行设置源视图。
第2行我用来将弹出窗口放在中间(我在SpriteKit中使用它,弹出窗口没有连接到任何东西)
第3行我用来删除箭头,因为我不想要它。
还有一行可选择使用
activityViewController.barButtonItem =
我没有使用,所以不是百分之百。
希望这有帮助
答案 1 :(得分:0)
适用于Swift 3/4 取决于@ crashoverride777答案
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.popoverPresentationController?.sourceView = self.view
activityVC.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
activityVC.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0) //Removes arrow as I dont want it
}