ios共享扩展帖子按钮标题

时间:2015-02-12 14:21:40

标签: ios ios8-share-extension

我想在SLComposeServiceViewController中更改帖子按钮的标题。 我设法得到了UIButton:

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

我尝试设置这样的标题:

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

但标题没有改变。

任何人都可以帮我吗?

我在iPad上看到了Evernote的扩展分享,它看起来像这样: enter image description here

更新

我的解决方案:

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

我有两个导航栏: 1."取消" \"保存"纽扣 2." back"按钮

我在导航到其他viewcontroller时更改了它们 (在我的情况下,我需要上传文件,用户需要从列表中选择位置)

注意:如果您未实施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];
}

它对我有用!!!

结果:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:10)

只需viewDidAppear

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

答案 1 :(得分:7)

您的代码:

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

...... 非常糟糕。它只能起作用,因为post按钮位于subviews中,是数组中的最后一项。但subviews的内容没有记录,可能随时更改。此外,由于此按钮没有公共API,因此完全有可能存在框架代码来阻止或覆盖对按钮文本的更改 - 所以即使我们假设您拥有正确的UI元素,您仍然可能无法更改它

Evernote的UI几乎可以肯定是一个完全自定义的设计,只有SLComposeServiceViewController。使用SLComposeServiceViewController不需要共享扩展名,这只是为了方便起见。如果它不能满足您的需求,请自行设计。

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