-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 回答