我想要导航栏上的两个自定义UIBarButtonItem
..这UiBarButtonItem
实际上应该是导航栏上两个UIBarButtonItem
的组合.. 这些按钮应该像UISegmentControl
..if一样相互组合,UISegmentControl
因为我需要上一个和下一个按钮。
那么是否可以组合或自定义两个类似的按钮,例如UISegmentControl
.
我想要导航栏上的两个自定义UIBarButtonItem
..这UiBarButtonItem
实际上应该是导航栏上两个UIBarButtonItem
的组合.. 这些按钮应该像UISegmentControl
..if一样相互组合,UISegmentControl
因为我需要上一个和下一个按钮。
那么是否可以组合或自定义两个类似的按钮,例如UISegmentControl
.
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];
这是我的粗略想法..如果您发现实施它有任何困难..让我知道
以下代码描述了如何添加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
}
试试这个代码我在一段时间之前从一些答案中得到了这个代码,我编辑它并在我的应用程序中使用它......试试这个..
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];
我希望这对你有帮助....
我希望这对你有帮助..
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];
}
}