-3

UILabelをNavigationBarのrightBarButtonItemとして作成する方法を教えてもらえますか?

UILabelを使用してUIViewでUIBarButtonItemのinitWithCustomViewを実行しようとしましたが、クラッシュします。

4

3 に答える 3

3

次のコードを試すことができます。

 UILabel *yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(260,6,55,32)];
  yourLabel.text = @"Ashu";
    [self.navigationController.navigationBar addSubview:yourLabel];

動作する場合はお知らせください。

于 2013-01-05T05:27:02.917 に答える
2

サンプルコードは次のとおりです。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 65.0, 30.0)];
label.text = @"Text";
barButton = [[UIBarButtonItem alloc] initWithCustomView:label];

次に、「barButton」を右側のナビゲーションアイテムと同じに設定するだけです。

于 2013-01-05T00:54:08.350 に答える
1

これを行うために:

yourButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30) ];
    [yourButton addTarget:self action:@selector(yourmethod:) forControlEvents:UIControlEventTouchUpInside];

    yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 8, 40, 30)];
    yourLabel.text = @"your Text";
    yourLabel.backgroundColor = [UIColor clearColor];
    yourLabel.textColor = [UIColor whiteColor];
    yourLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    [yourLabel sizeToFit];
    [yourButton addSubview:yourLabel];

[yourButton sizeToFit]; 

    yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:yourButton];
     self.navigationItem.rightBarButtonItem = yourBarButton;
于 2013-01-05T08:10:28.487 に答える