以编程方式将UILabel添加到工具栏

C.J*_*hns 1 iphone uitableview uitoolbar uilabel ios

我试图以UILabel编程方式添加到我的,UIToolBar但它似乎似乎没有出现.这就是我对我的代码所做的事情.

- (void) viewWillAppear:(BOOL)animated 
{
    // Create custom toolbar at top of screen under navigation controller
    [matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];  
    matchingSeriesInfoToolBar = [UIToolbar new];
    [matchingSeriesInfoToolBar sizeToFit];
    CGFloat toolbarHeight = 30;
    CGRect mainViewBounds = [[UIScreen mainScreen] applicationFrame];
    [matchingSeriesInfoToolBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 0, CGRectGetWidth(mainViewBounds), toolbarHeight)];
    matchingSeriesInfoToolBar.tintColor = [UIColor darkGrayColor];
    [self.view addSubview:matchingSeriesInfoToolBar];

    // Create size of uitableview (to fit toolbar.
    [matchingSeriesTableView setFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30)];
    [self.view addSubview:matchingSeriesTableView];

    // Ad UILabel to the toolbar
    UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:manufSelectionLabel];
    matchingSeriesInfoToolBar.items = [NSArray arrayWithObject:textFieldItem];
    manufSelectionLabel.text = @"Hello World!";

    [super viewWillAppear:animated];
}
Run Code Online (Sandbox Code Playgroud)

所以我已经创建了一个自定义工具栏,我已经更改了屏幕底部的常用位置,显示在下面UINavigationController,这也添加到视图中,因此它在视图过渡中正确动画.

之后我创建了tableview的大小,以便它出现在自定义工具栏之后.

然后从那里我试图添加UILabel到工具栏..但由于某种原因它没有工作.

任何帮助将不胜感激.

sc0*_*10n 8

你必须在某处创建标签.这段代码工作得很好.

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolbar];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
toolbar.items = [NSArray arrayWithObject:item];

label.text = @"Hello World";
Run Code Online (Sandbox Code Playgroud)