ios 分享扩展 post 按钮标题

ios share extension post button title

我想更改 SLComposeServiceViewController 中 post 个按钮的标题。 我设法获得了 UIButton:

NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];

我试过这样设置标题:

[postButton setTitle:@"Save" forState:UIControlStateNormal];

但标题没有改变。

谁能帮我解决这个问题?

我在 iPad 上看到了 Evernote 的共享扩展,它看起来像这样:

更新

我的解决方案:

我找到了我的问题的解决方案,我删除了原来的导航栏并创建了自定义导航栏。

我有两个导航栏: 1. 使用 "cancel"\"save" 按钮 2. 使用 "back" 按钮

我会在导航到其他 viewcontroller 时更改它们 (在我的例子中,我需要上传文件并且用户需要列表中的 select 位置)

注意:如果您不实施 configurationItems,您只需要第一个导航栏。 (只需调用从 viewDidAppear

设置自定义导航栏

所以我的代码在这里:

@property (strong, nonatomic) UINavigationBar *customNavBar;

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];  
    self.customNavBar = [[UINavigationBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
    [self.navigationController.navigationBar removeFromSuperview];
    [self.navigationController.view addSubview:self.customNavBar];
    [self setCancelSaveNavigationItem];
}

setCancelSaveNavigationItem--> 从 shareViewController 的 viewDidAppear 调用

-(void)setCancelSaveNavigationItem
{
    UINavigationItem *newItem = [[UINavigationItem alloc] init];
    UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",nil)  style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonTapped:)];
    UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done",nil)  style:UIBarButtonItemStyleDone target:self action:@selector(saveButtonTapped:)];
    newItem.leftBarButtonItem = cancelBarButtonItem;
    newItem.rightBarButtonItem = saveBarButtonItem;
    [self.customNavBar setItems:@[newItem]];
    [self.navigationItem setBackBarButtonItem:cancelBarButtonItem];
    [self.navigationItem setRightBarButtonItem:saveBarButtonItem];
    if(self.item.value == nil){
        saveBarButtonItem.enabled = NO;
    }
}

setBackNavigationItem--> 在 configurationItems 中调用 --> 在 self.item.tapHandler 函数中调用

-(void)setBackNavigationItem
{
    UINavigationItem *newItem = [[UINavigationItem alloc] init];
    UIBarButtonItem *selectBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Select",nil)  style:UIBarButtonItemStylePlain target:self action:@selector(selectButtonTapped:)];
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"❮ %@", NSLocalizedString(@"Back",nil)]  style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped:)];
    newItem.leftBarButtonItem = backBarButtonItem;
    newItem.rightBarButtonItem = selectBarButtonItem;
    [self.customNavBar setItems:@[newItem]];
    [self.navigationItem setBackBarButtonItem:backBarButtonItem];
    [self.navigationItem setRightBarButtonItem:selectBarButtonItem];
}

点击手柄按钮:

- (void)backButtonTapped:(id)sender {
    if([self.navigationController.viewControllers count] ==2){
        [self setCancelSaveNavigationItem];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)cancelButtonTapped:(id)sender {
    [self cancel];
}

- (void)selectButtonTapped:(id)sender {
    ...
    [self setCancelSaveNavigationItem];
    [self popConfigurationViewController];
}

- (void)saveButtonTapped:(id)sender {
    ...
    [self cancel];
}

这对我有用!!!

结果:

您的代码:

NSArray* subviews =[self.navigationController.navigationBar subviews];
UIButton* postButton =[subviews lastObject];

...是一个非常糟糕的主意。它之所以有效,是因为 post 按钮位于 subviews 中,并且是数组中的最后一项。但 subviews 的内容未记录在案,可能随时更改。此外,由于此按钮没有 public API,因此完全有可能存在框架代码来防止或覆盖对按钮文本的更改——所以即使我们假设您拥有正确的 UI 元素,您可能仍然无法更改它。

Evernote 的 UI 几乎可以肯定是完全定制的设计,仅类似于 SLComposeServiceViewController。共享扩展不需要使用 SLComposeServiceViewController,那只是为了方便。如果不能满足您的需求,请自行设计。

更新:出于好奇,我解压了 Evernote IPA 并查看了 EvernoteShare.appexnm。没有提及 SLComposeServiceViewController,这证实 Evernote 没有在其扩展中使用 class。

就在viewDidAppear

self.navigationController?.navigationBar.topItem?.rightBarButtonItem?.title = "Save"