我正在尝试在工具栏中添加标签.按钮效果很好,但是当我添加标签对象时,它会崩溃.有任何想法吗?
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)
Mat*_*t R 119
对于那些使用Interface Builder进行布局的人来说UIToolBar,也可以单独使用Interface Builder来完成.
要添加a UILabel,UIToolBar您需要通过拖动新对象UIView来UIToolBar在IB中添加通用对象.将自动创建一个将使用您的自定义初始化的.接下来添加到和编辑图形,以配合您的首选风格.然后,您可以根据需要直观地设置固定和/或可变间隔垫,以适当地定位.UIViewUIToolBarIBUIBarButtonItemUIViewUILabelUIViewUILabelUILabel
你还必须设置两者的背景UILabel和UIView以clearColor获得UIToolBar通过正确下显示UILabel.
Fré*_*dda 29
我发现answerBot的答案非常有用,但我认为我在Interface Builder中找到了一种更简单的方法:


将此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()每次更新标签文本时都必须调用
我正在使用这个技巧的一个方面是实例化一个UIActivityIndicatorView最好的UIToolBar东西,否则是不可能的.比如我在这里有一个UIToolBar2 UIBarButtonItem,a FlexibleSpaceBarButtonItem,然后是另一个UIBarButtonItem.我想插入UIActivityIndicatorView到UIToolBar灵活空间和最终(右手)按钮之间.所以,在我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)
| 归档时间: |
|
| 查看次数: |
95373 次 |
| 最近记录: |