How to change Edit/Done button title in UINavigationBar

前端 未结 3 2277
执念已碎
执念已碎 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:37

    You can change the title of the edit button like this:-

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        // Make sure you call super first
        [super setEditing:editing animated:animated];
    
        if (editing)
        {
            self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
        }
        else
        {
            self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
        }
    }
    

    its working like edit:-

    enter image description here

    TO

    enter image description here

    0 讨论(0)
  • 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];
    
    0 讨论(0)
  • 2021-02-20 07:01

    Here is the method to change it for Swift

    override func setEditing (editing:Bool, animated:Bool)
    {
        super.setEditing(editing,animated:animated)
        if (self.editing) {
            self.editButtonItem().title = "Editing"
        }
        else {
            self.editButtonItem().title = "Not Editing"
        }
    }
    
    0 讨论(0)
提交回复
热议问题