工具栏项目未显示

时间:2011-08-04 10:29:47

标签: iphone toolbar

不确定我做错了什么。我没有笔尖所以我在loadView中制作了所有内容。工具栏显示但segmentedControl不显示。

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

2 个答案:

答案 0 :(得分:1)

用以下代码代替代码;这将帮助您将片段控件添加到工具栏:

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

答案 1 :(得分:0)

您必须像使用工具栏一样将其添加为视图的subview。即:

[self.view addSubview:segmentedControl];

它应该有用。

干杯