简体   繁体   中英

Custom UIBarButtonItem on Navigation bar

I want a two Custom UIBarButtonItem on Navigation bar ..this UiBarButtonItem should be actually combination of two UIBarButtonItem on Navigation bar.. and these button should be combine to each other similar like the UISegmentControl ..if can not UISegmentControl because i needed for the previous and next button.

So is it possible to combine or customize two button similar like the UISegmentControl .

image like this

CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
fieldAccessoryView.tag = 200;

[fieldAccessoryView setBarStyle:UIBarStyleBlack];

UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
[segmentButton release];
[spaceButton release];
[doneButton release];
[segmentedControl release];
I hope this help you..

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                        [NSArray arrayWithObjects:
[NSString stringWithString:@"First"],
[NSString stringWithString:@"Second"],
nil]];


//just customize this

[segmentControl setImage:[UIImage imageNamed:@"YOUR_IMAGE1.png"] forSegmentAtIndex:0];
[segmentControl setImage:[UIImage imageNamed:@"YOUR_IMAGE2.png"] forSegmentAtIndex:1];


[segmentedControl setFrame:[self.navigationController.toolbar bounds]];

[self.navigationController.toolbar addSubview:segmentedControl];
[segmentedControl release];

It is rough idea from me .. If you found any difficulties to implement it.. let me know

Following code describe how can add UIBarButtonItem in UINavigationBar .

In your case add UIBarButtonItem with UIBarButtonSystemItemFlexibleSpace that look like UISegmentControl.

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [Toolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
    [barItems addObject:btnDone];

    [Toolbar setItems:barItems animated:YES];

UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:Toolbar];
self.navigationItem.rightBarButtonItem = twoButtons;

// Method of UIBarButtonItem

-(void)Cancel
{
  // Write Code for Cancel Method
}

-(void)done
{
  // Write Code for Done Method
}

try this code i get this code from some answer before some time and i edit it and use it in my application... try this..

NSArray *segItemsArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;// set your default selected button(view)

UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];

[self setToolbarItems:barArray];
[self.navigationController setToolbarHidden:NO animated:YES];

i hope this helpful to you....

I hope this help you..

UISegmentedControl *tabNavigation = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
tabNavigation.segmentedControlStyle = UISegmentedControlStyleBar;
[tabNavigation setEnabled:YES forSegmentAtIndex:0];
[tabNavigation setEnabled:YES forSegmentAtIndex:1];
tabNavigation.momentary = YES;
[tabNavigation addTarget:self action:@selector(segmentedControlHandler:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *barSegment = [[UIBarButtonItem alloc] initWithCustomView:tabNavigation];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

 keyboardDoneButtonView = [[UIToolbar alloc] init ];//WithFrame:CGRectMake(0, 480, 320, 44)];

 keyboardDoneButtonView.barStyle = UIBarStyleBlackTranslucent;
 keyboardDoneButtonView.translucent = YES;
 [keyboardDoneButtonView setFrame:CGRectMake(0, 480, 320, 44)];
 [keyboardDoneButtonView sizeToFit];
 [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:barSegment,flexSpace,doneButton, nil]];

Example text :

 self.txtGivenName.inputAccessoryView=keyboardDoneButtonView;
 self.txtLastName.inputAccessoryView=keyboardDoneButtonView;



- (void)segmentedControlHandler:(id)sender
 {
if (sender){
    switch ([(UISegmentedControl *)sender selectedSegmentIndex]) {
        case 0:
        {
            [self previousClicked]; 
            break;
        }
        case 1:
        {
            [self nextClicked];
            break;
        }
        default:
            break;
    }
}

}

  -(void)previousClicked{
UIResponder* nextResponder = [sclView viewWithTag:selected-1];
if (nextResponder)
    {
    [nextResponder becomeFirstResponder];
    }

}

  -(void)nextClicked{
UIResponder* nextResponder = [sclView viewWithTag:selected+1];
if (nextResponder)
    {
    [nextResponder becomeFirstResponder];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM