Toolbar item not showing
Not sure what I'm doing wrong. I don't have a nib so I'm making everything in loadView. The toolbar shows up but the segmentedControl do开发者_开发技巧es not.
- (void)loadView
{
// Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
NSArray *toolbarItems = [NSArray arrayWithObjects:item, nil];
[toolbar setItems:toolbarItems animated:NO];
[self.view addSubview:toolbar];
}
Write below code in place of your code; this will help you to add segment control to your toolbar:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
[toolbar setTintColor:[UIColor lightGrayColor]];
CGRect frame;
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.width = 200;
frame.size.height = 30;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Hello",@"Hi", nil]];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.frame = frame;
[toolbar addSubview:segmentedControl];
[self.view addSubview:toolbar];
You've got to add it as a subview
of the view as you did with the toolbar. I.e:
[self.view addSubview:segmentedControl];
It should work.
Cheers
精彩评论