How to change Edit/Done button title in UINavigationBar

前端 未结 3 2276
执念已碎
执念已碎 2021-02-20 06:26

If Editing mode is enabled for the tableview it shows a button titled Edit, when pressing it it changes the title to done

i was wondering if there is a way to change the

3条回答
  •  失恋的感觉
    2021-02-20 06:58

    Building on Nitin's answer I suggest a slightly different approach that uses the built in UIButtonBar system items.

    This will give your UI the system look & feel. For example, the standard "Done" button to cease editing should have a specific bold look on iOS 8. Apple might change these stylings in future. By using the system defined buttons your app will pick up the current aesthetic automatically.

    This approach also provides you with free string localisation. Apple have already translated the system button titles for you in to the dozens of languages that iOS supports.

    Here's the code I've got:

    -(IBAction) toggleEditing:(id)sender
    {
      [self setEditing: !self.editing animated: YES];
    }
    
    -(void) setEditing:(BOOL)editing animated:(BOOL)animated
    {
      [super setEditing: editing animated: animated];
    
      const UIBarButtonSystemItem systemItem = 
        editing ? 
        UIBarButtonSystemItemDone : 
        UIBarButtonSystemItemEdit;
    
      UIBarButtonItem *const newButton = 
        [[UIBarButtonItem alloc] 
          initWithBarButtonSystemItem: systemItem 
                               target: self 
                               action: @selector(toggleEditing:)];
    
      [self.navigationItem setRightBarButtonItems: @[newButton] animated: YES];
    }
    

    The example here is for the case where your UIViewController is hosted in a UINavigationController and so has a UINavigationItem instance. If you're not doing this, you'll need to update the bar item in an appropriate way.

    In your viewDidLoad use the following call to configure the edit button ready for use:

    [self setEditing: NO animated: NO];
    

提交回复
热议问题