导航栏上的自定义UIBarButtonItem

时间:2013-02-25 09:46:21

标签: iphone ios uibutton custom-controls

我想在导航栏上有两个自定义UIBarButtonItem ..这个UiBarButtonItem实际上应该是导航栏上两个UIBarButtonItem的组合..而且这些按钮应该相互结合相似比如UISegmentControl ..如果不能UISegmentControl,因为我需要上一个和下一个按钮。

因此可以组合或自定义两个类似于UISegmentControl的按钮。

image like this

5 个答案:

答案 0 :(得分:1)

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];

答案 1 :(得分:0)

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];

这是我的粗略想法..如果你发现任何困难实现它..让我知道

答案 2 :(得分:0)

以下代码介绍了如何在UIBarButtonItem中添加UINavigationBar

在您的情况下添加 UIBarButtonItem UIBarButtonSystemItemFlexibleSpace 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;

// UIBarButtonItem的方法

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

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

答案 3 :(得分:0)

尝试这段代码我会在一段时间之前从某些答案中获取此代码并编辑它并在我的应用程序中使用它...试试这个..

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];

我希望这对你有帮助....

答案 4 :(得分:0)

我希望这可以帮助你......

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]];

示例文字:

 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];
    }

}