我试图在Facebook SDK中创建自定义共享按钮,但我遇到了问题
let content = FBSDKShareLinkContent()
content.contentTitle = "(postTitle)"
content.contentURL = NSURL(string:"(www)")
let shareButton = FBSDKShareButton()
shareButton.shareContent = content
shareButton.center = selfview.center
self.view.addSubiview(shareButton)
它工作,但我有一个默认按钮,在我的布局中看起来不太好。我想使用我自己的按钮facebookShareButton: UIButton
来分享我的内容。我有可能在自己的按钮中调用此操作吗?如果是,我该怎么做?
我有一个解决这个问题的方法:
我们需要创建一个UIButton出口,例如@IBOutlet weak var facebookOutlet: UIButton!
,然后在viewDidLoad()
中添加代码
facebookOutlet.addTarget(self, action: #selector(ViewController.postFacebook(_:)), forControlEvents: .TouchUpInside)
然后在viewDidAppear()
中,我们需要添加:
let content = FBSDKShareLinkContent()
content.contentTitle = "(postTitle)"
content.contentURL = NSURL(string:"(www)")
let shareButton = FBSDKShareButton()
shareButton.shareContent = content
shareButton.center = self.view.center
self.view.addSubview(shareButton)
然后在操作我们的按钮时,我们需要添加:
func postFacebook(sender: UIButton){
shareButton.sendActionsForControlEvents(.TouchUpInside)
}
它非常完美,允许我们创建一个自定义的facebook按钮。
使用最新的FBSDKShareKit,您可以将以下内容添加到自定义按钮的动作处理程序中
let dialog = ShareDialog()
dialog.shareContent = content
dialog.show()