rs2000
rs2000

Reputation: 147

Replace right UIBarButtonItem on Navigation controller with "Done" button

I've looked at lots of articles to help me but could not find the answer...

I would like to replace my right bar button when the keyboard is active with a "Done" button and then replace it back when the editing has finished...

I already have a right bar button which is a segmented control and is setup when the view loads. I am not sure if I can switch the bar button once it is created ?

The following is some screen shots...

Would like to change the Prev / Next Button to a done button when keyboard appears

Would like to change the Prev / Next Button to a done button when keyboard appears

Screen with Keyboard

The following is my code that triggers when the keyboard is displayed... The code executes but the button is NOT displayed...

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Replace the Right Navigation Button with done button

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(doneEditing)];
self.navigationItem.rightBarButtonItem = doneButton;

// Slide up the view

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect rect = self.view.frame;

// Move the view .... Note need to calculate this based on the size of the keyboard !!

rect.origin.y -= 120.0f;
rect.size.height += 120.f;

self.view.frame = rect;

// Resize the text view .... Note need to calculate this based on screen size !!

CGSize frameSize = self.reviewTextView.frame.size;
CGPoint framePos = self.reviewTextView.frame.origin;
[self.reviewTextView setFrame:CGRectMake(framePos.x, framePos.y, frameSize.width, 200.0f)];

[UIView commitAnimations];

return YES;
}

The following is the code segment for setting the prev / next button when the view is loaded

    //set up the segmented control and add it to the nav bar rightBartButtonItem

UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"\U000025C0\U0000FE0E Prev", @"Next \U000025B6\U0000FE0E", nil]];
UIBarButtonItem * segmentControlButton = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
[segmentControl setBackgroundColor:[UIColor clearColor]];
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.frame = CGRectMake(0, 0, 120, 30);
[segmentControl setMomentary:YES];
[segmentControl addTarget:self
                   action:@selector(onSegmentChanged:)
         forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = segmentControlButton;

Upvotes: 1

Views: 669

Answers (3)

rs2000
rs2000

Reputation: 147

I am sorry but this is a coding error on my part...

The support from User MICANTOX You triggered a thought that I have the correct code but in the wrong place !!! Due to this being a sub-view that I was not executing the code in the right place.... The code has now been placed in the parent view

Upvotes: 0

Nicholas Smith
Nicholas Smith

Reputation: 11754

Sometimes you need to prompt the system to redraw the bar with the changes, try calling [self.navigationController.navigationBar setNeedsDisplay].

Upvotes: 1

Chanuka
Chanuka

Reputation: 199

Try this one,

if(keyboardIsActive){
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done"
                                   style:UIBarButtonItemStyleBordered
                                   target:self.revealViewController
                                   action:@selector(revealToggle:)];

    destinationViewController.navigationItem.leftBarButtonItem = menuButton;}

Upvotes: 0

Related Questions