如何创建标准的 iOS 共享按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19736440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:43:28  来源:igfitidea点击:

How do I create a standard iOS Share button?

ioscocoa-touchios7uikitshare

提问by Peter Hosey

The iOS Human Interface Guidelines say:

iOS人机界面指南说

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 documentationturns up nothing useful.

好的,但是我如何“使用系统提供的共享按钮”?搜索文档没有发现任何有用的信息。

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

我已经收集到我应该在响应被点击的按钮时使用 UIActivityViewController,但是首先我将如何创建标准的共享按钮?

回答by Peter Hosey

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.

标准的分享按钮是一个 UIBarButtonItem(所以它只能出现在导航栏或工具栏上)。您需要创建一个“系统项”;具体来说,是一个“行动项目”。“操作”栏按钮项是共享按钮。

回答by SimpleApp

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

这是代码。我假设您在 ViewController 中,因此 self 具有 navigationItem 属性。

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

回答by danielrosero

This goes in your viewDidLoad:

这在您的 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"):

并定义要作为操作调用的选择器方法(在我的情况下命名为“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];
}

回答by saneryee

Main.storyboard -> Bar Button Item -> Inspector -> System Item chose "Action"enter image description here

Main.storyboard -> Bar Button Item -> Inspector -> System Item 选择“Action”在此处输入图片说明

回答by jarora

Here is the code for Swift 3.

这是 Swift 3 的代码。

func addShareBarButtonItem() {

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

    self.navigationItem.rightBarButtonItem = shareButton
}

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

回答by dzensik

The system-provided Action button can also be fully created with an Interface Builder. To do so just drag & drop the UIToolbarto your view. Than drag & drop the UIBarButtonIteminto 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!

系统提供的 Action 按钮也可以完全使用 Interface Builder 创建。为此,只需将UIToolbar拖放到您的视图中即可。将UIBarButtonItem拖放到 UIToolbar 中。下一步在视图层次结构中选择UIBarButtonItem,转到 Attribute Inspector 并选择“Action”作为系统项。完毕!

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

此外,可以将UIBarButtonItem与您的类连接为 IBOutlet 或 IBAction。

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

请注意:我使用 Xcode 7 作为参考。

回答by ingconti

only my two cents for swift 4 / Xcode 10:

swift 4 / Xcode 10 只有我的两美分:

1) action has initial char changed:

1) 动作的初始字符已更改:

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

2) add @obj:

2)添加@obj:

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