How do I create a standard iOS Share button?

感情迁移 提交于 2019-12-18 12:45:19

问题


The iOS Human Interface Guidelines say:

Use the system-provided Share button. Users are familiar with the meaning and behavior of this button, so it’s a good idea to use it when possible. The main exception to this is if your app does not contain a toolbar or navigation bar[, as] the Share button can only be used in a toolbar or navigation bar.

OK, but how do I “use the system-provided Share button”? A search of the documentation turns up nothing useful.

I've gathered that I should use UIActivityViewController in my response to the button being tapped, but how would I create the standard Share button in the first place?


回答1:


The standard Share button is a UIBarButtonItem (so it can only go on a navigation bar or toolbar). You need to create a “system item”; specifically, an “action item”. The “action” bar button item is the Share button.




回答2:


Here is the code. I am assuming you are inside ViewController so self has navigationItem property.

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                     target:self
                     action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;



回答3:


This goes in your viewDidLoad:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(compartir:)];
self.navigationItem.rightBarButtonItem = shareButton;

And define your selector method to be called as action (in my case named as "compartir"):

- (void) compartir:(id)sender{

//Si no


NSLog(@"shareButton pressed");


NSString *stringtoshare= @"This is a string to share";
UIImage *imagetoshare = img; //This is an image to share.

NSArray *activityItems = @[stringtoshare, imagetoshare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:YES completion:nil];
}



回答4:


Main.storyboard -> Bar Button Item -> Inspector -> System Item chose "Action"




回答5:


The system-provided Action button can also be fully created with an Interface Builder. To do so just drag & drop the UIToolbar to your view. Than drag & drop the UIBarButtonItem into the UIToolbar. As next step select in the view hierarchy the UIBarButtonItem, go to the Attribute Inspector and select as System Item the "Action". Done!

Additionally it is possible to connect the UIBarButtonItem with your class as IBOutlet or IBAction.

Please note: I use as a reference an Xcode 7.




回答6:


Here is the code for Swift 3.

func addShareBarButtonItem() {

    let shareButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: #selector(MyViewController.shareButtonPressed))

    self.navigationItem.rightBarButtonItem = shareButton
}

func shareButtonPressed() {
    //Do something now!
}



回答7:


only my two cents for swift 4 / Xcode 10:

1) action has initial char changed:

let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonPressed))

2) add @obj:

@objc func shareButtonPressed() {
    //Do something now!
}


来源:https://stackoverflow.com/questions/19736440/how-do-i-create-a-standard-ios-share-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!