【发布时间】:2013-05-02 12:43:05
【问题描述】:
我想精确控制添加到 UINavigationController 工具栏的自定义视图。更具体地说.. 我想在我的工具栏中显示一个 UILable ontop 项目。
我有一个toolbarItems,最初设置了一些UIBarButtonItems。我试图达到的效果是以编程方式扩展工具栏的高度,然后在其余按钮的顶部显示一个UILabel ..这就是我目前拥有的:
-(void)expandToolBar:(NSString *)title {
UIToolbar* toolBar =self.navigationController.toolbar;
CGRect toolbarFrame = toolBar.frame;
[UIView animateWithDuration:0.25f delay:0
options:UIViewAnimationOptionLayoutSubviews animations:^{
// expand toolbar frame vertically
[toolBar setFrame:
CGRectMake(toolbarFrame.origin.x,
toolbarFrame.origin.y-15,
toolbarFrame.size.width,
toolbarFrame.size.height + 15)];
} completion:^(BOOL finished){
[UIView animateWithDuration:0.50f animations:^{
// some code here to move the existing toolbar items lower
// ie to make space for the label
UILabel* label = [[UILabel alloc] initWithFrame:labelFrame];
[label setBackgroundColor:[UIColor clearColor]];
label.text = title;
UIBarButtonItem *labelItem = [[UIBarButtonItem alloc]
initWithCustomView:label];
// add label to toolbar
NSMutableArray *newItems = [self.toolbarItems mutableCopy];
[newItems addObject:labelItem];
self.toolbarItems = newItems;
}];
}];
}
这样做的结果是所有现有按钮都被压扁,而标签取而代之。问题是,如果我尝试变得太有创意并开始手动弄乱工具栏的子视图,我就会开始徘徊在未记录的 API 领域,somethingApple 不会tolerate。想法?
【问题讨论】:
标签: iphone objective-c cocoa-touch uinavigationcontroller uinavigationbar