1

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

画面の下部から通常の場所を変更して、の下に表示されるカスタムツールバーを作成しましたUINavigationController。これもこのようにビューに追加されるため、ビューの遷移で適切にアニメーション化されます。

その後、カスタムツールバーの後に表示されるようにテーブルビューのサイズを作成します。

次に、そこからツールバーにを追加しようとしてUILabelいますが、何らかの理由で機能しません。

どんな助けでも大歓迎です。

4

2 に答える 2

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";
于 2012-05-21T21:50:30.773 に答える
0

投稿したコードでは、を作成することはありませんUILabel。あなたのコメントはツールバーに広告UILabelをUIBarButtonItem言います、しかしあなたはそれからカスタムビューでを作ることに進みますmanufSectionLabel

作成するコードはどこにありますmanufSectionLabelか?


PSこの行は何もしません:

[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];

その時点でmatchingSeriesInfoToolbarはnil-まだ作成していないからです!

于 2012-05-21T21:43:11.257 に答える