How do I create a standard iOS Share button?

后端 未结 7 481
情话喂你
情话喂你 2020-12-30 19:13

The iOS Human Interface Guidelines say:

Use the system-provided Share button. Users are familiar with the meaning and behavior of thi

相关标签:
7条回答
  • 2020-12-30 19:20

    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];
    }
    
    0 讨论(0)
  • 2020-12-30 19:29

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

    0 讨论(0)
  • 2020-12-30 19:33

    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!
    }
    
    0 讨论(0)
  • 2020-12-30 19:36

    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!
    }
    
    0 讨论(0)
  • 2020-12-30 19:37

    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.

    0 讨论(0)
  • 2020-12-30 19:41

    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.

    0 讨论(0)
提交回复
热议问题