将UILabel添加到UIToolbar

96 iphone uibarbuttonitem ios

我正在尝试在工具栏中添加标签.按钮效果很好,但是当我添加标签对象时,它会崩溃.有任何想法吗?

UIBarButtonItem *setDateRangeButton = [[UIBarButtonItem alloc] initWithTitle:@"Set date range"
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(setDateRangeClicked:)];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
label.text = @"test";

[toolbar setItems:[NSArray arrayWithObjects:setDateRangeButton,label, nil]];

// Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];

// Reload the table view
[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

ada*_*dam 128

看看这个

[[UIBarButtonItem alloc] initWithCustomView:yourCustomView];
Run Code Online (Sandbox Code Playgroud)

基本上每个项目都必须是"按钮",但它们可以使用您需要的任何视图进行实例化.这是一些示例代码.请注意,由于其他按钮通常位于工具栏上,因此将间隔符放置在标题按钮的每一侧,使其保持居中.

NSMutableArray *items = [[self.toolbar items] mutableCopy];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer];
[spacer release];

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];
[spacer2 release];

UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:self.titleLabel];
[items addObject:title];
[title release];

[self.toolbar setItems:items animated:YES];
[items release];
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您选择使用此路线,则必须适当地设置标签样式(label.backgroundColor = [UIColor clearColor]等).你也可以初始化一个UIBarButtonItem样式为Plain,它会给你一个类似的外观 (10认同)
  • 你不想在标题按钮栏项后添加第二个间隔符吗? (7认同)

Mat*_*t R 119

对于那些使用Interface Builder进行布局的人来说UIToolBar,也可以单独使用Interface Builder来完成.

要添加a UILabel,UIToolBar您需要通过拖动新对象UIViewUIToolBar在IB中添加通用对象.将自动创建一个将使用您的自定义初始化的.接下来添加到和编辑图形,以配合您的首选风格.然后,您可以根据需要直观地设置固定和/或可变间隔垫,以适当地定位.UIViewUIToolBarIBUIBarButtonItemUIViewUILabelUIViewUILabelUILabel

你还必须设置两者的背景UILabelUIViewclearColor获得UIToolBar通过正确下显示UILabel.

  • 我似乎无法让这个工作.当我将UIView拖到工具栏上时,它最终被放置在视图控制器内,而不是放在工具栏上.另外,我们在导航控制器或视图控制器中执行此操作吗? (4认同)
  • 很好的技巧,不知道你可以在Interface Builder中做到这一点.非常感谢你. (2认同)

Fré*_*dda 29

我发现answerBot的答案非常有用,但我认为我在Interface Builder中找到了一种更简单的方法:

  • 创建一个UIBarButtonItem并将其添加到Interface Builder中的工具栏

在此输入图像描述

  • 取消选中此BarButtonItem的"已启用"

在此输入图像描述

  • 将此BarButtonItem插入到类中的属性(这是在Swift中,但在Obj-C中非常相似):

    @IBOutlet private weak var lastUpdateButton: UIBarButtonItem! // Dummy barButtonItem whose customView is lastUpdateLabel
    
    Run Code Online (Sandbox Code Playgroud)
  • 为Label本身添加另一个属性:

    private var lastUpdateLabel = UILabel(frame: CGRectZero)
    
    Run Code Online (Sandbox Code Playgroud)
  • 在viewDidLoad中,添加以下代码以设置标签的属性,并将其添加为BarButtonItem的customView

    // Dummy button containing the date of last update
    lastUpdateLabel.sizeToFit()
    lastUpdateLabel.backgroundColor = UIColor.clearColor()
    lastUpdateLabel.textAlignment = .Center
    lastUpdateButton.customView = lastUpdateLabel
    
    Run Code Online (Sandbox Code Playgroud)
  • 要更新UILabel文本:

    lastUpdateLabel.text = "Updated: 9/12/14, 2:53"
    lastUpdateLabel.sizeToFit() 
    
    Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

lastUpdateLabel.sizetoFit()每次更新标签文本时都必须调用


Ala*_*lan 6

我正在使用这个技巧的一个方面是实例化一个UIActivityIndicatorView最好的UIToolBar东西,否则是不可能的.比如我在这里有一个UIToolBar2 UIBarButtonItem,a FlexibleSpaceBarButtonItem,然后是另一个UIBarButtonItem.我想插入UIActivityIndicatorViewUIToolBar灵活空间和最终(右手)按钮之间.所以,在我RootViewController做我的以下,

- (void)viewDidLoad {
[super viewDidLoad];// Add an invisible UIActivityViewIndicator to the toolbar
UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:767];
NSArray *items = [toolbar items];

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    

NSArray *newItems = [NSArray arrayWithObjects:[items objectAtIndex:0],[items objectAtIndex:1],[items objectAtIndex:2],
                     [[UIBarButtonItem alloc] initWithCustomView:activityIndicator], [items objectAtIndex:3],nil];
[toolbar setItems:newItems];}
Run Code Online (Sandbox Code Playgroud)