我是按照UINavigationBar设计的,但我是否有可能添加如下所示的segmentControl?实现这一目标的最佳途径是什么?
答案 0 :(得分:0)
您可以按如下方式模仿您的要求:
-(void) segmentAction: (UISegmentedControl *) segmentedControl
{
// Update the label with the segment number
NSString *segmentNumber = [NSString stringWithFormat:@"%0d",
segmentedControl.selectedSegmentIndex + 1];
[(UITextView *)self.view setText:segmentNumber];
}
- (void) loadView
{
[super loadView];
// Create a central text view
UITextView *textView = [[UITextView alloc]
initWithFrame:self.view.frame];
textView.font = [UIFont fontWithName:@"Futura" size:96.0f];
textView.textAlignment = UITextAlignmentCenter;
self.view = textView;
// Create the segmented control
NSArray *buttonNames = [NSArray arrayWithObjects:
@"Recent", @"Popular", @"My", nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc]
initWithItems:buttonNames];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
[segmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
// Add it to the navigation bar
self.navigationItem.titleView = segmentedControl;
}
或者有另一种方法如下:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(@"Recent", @"")],
[NSString stringWithString:NSLocalizedString(@"Popular", @"")],
[NSString stringWithString:NSLocalizedString(@"My", @"")],
nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor clearColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:@selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:[self.navigationController.toolbar bounds]];
[self.navigationController.toolbar addSubview:segmentedControl];