我希望在我的应用程序中有一个按钮,当按下该按钮时,将弹出此按钮(请参见下图)。我怎么做?我不想创建一个自定义共享活动,我只想要默认一个?我使用什么代码?在线上的所有教程都在Objective- C中。请尽快给出答案。
图片:http: //9to5mac.com/2014/06/30/hands-on-1password-beta-shows-off- ios-8s-touch-id-extensions-apis-video/#jp- carousel-330420
到目前为止,这是我的代码,但是我得到一个错误,即UIBarButtonItem无法转换为UIVIew为什么?该动作是否连接到导航栏按钮项目?
@IBAction func ActionButton(sender: UIBarButtonItem) { let firstActivityItem = "Text you want" let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")! // If you want to put an image let image : UIImage = UIImage(named: "TALogo")! let activityViewController : UIActivityViewController = UIActivityViewController( activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil) // This lines is for the popover you need to show in iPad activityViewController.popoverPresentationController?.sourceView = (sender as! UIBarButtonItem) // This line remove the arrow of the popover to show in iPad activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) // Anything you want to exclude activityViewController.excludedActivityTypes = [ UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo ] self.presentViewController(activityViewController, animated: true, completion: nil) }
该方式UIActivityViewController例如以以下方式使用:
UIActivityViewController
@IBAction func shareSheet(sender: AnyObject) { let firstActivityItem = "Text you want" let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")! // If you want to put an image let image : UIImage = UIImage(named: "image.jpg")! let activityViewController : UIActivityViewController = UIActivityViewController( activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil) // This lines is for the popover you need to show in iPad activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton) // This line remove the arrow of the popover to show in iPad activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) // Anything you want to exclude activityViewController.excludedActivityTypes = [ UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo ] self.presentViewController(activityViewController, animated: true, completion: nil) }
上面的代码适用于iPhone和iPad,因为popoverPresentationController在iOS 8中设置了新代码后,它也适用于iPad。
popoverPresentationController
在使用的情况下,UIBarButtonItem您需要替换以下行:
UIBarButtonItem
activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)
有了这个:
activityViewController.popoverPresentationController?.barButtonItem = (sender as! UIBarButtonItem)
希望对您有所帮助。