Iphone 控制添加到工具栏的视图项的显示坐标

Iphone 控制添加到工具栏的视图项的显示坐标,iphone,objective-c,cocoa-touch,uinavigationcontroller,uinavigationbar,Iphone,Objective C,Cocoa Touch,Uinavigationcontroller,Uinavigationbar,我想对添加到UINavigationController工具栏的自定义视图进行精确控制。更具体地说。。我想在我的工具栏中显示一个可编辑的项目列表 我有一个toolbarItems,最初是用一些uibarbuttonims设置的。我试图实现的效果是通过编程扩展工具栏的高度,然后在其余按钮的顶部显示UILabel。。这就是我目前拥有的: -(void)expandToolBar:(NSString *)title { UIToolbar* toolBar =self.navigationC

我想对添加到UINavigationController工具栏的自定义视图进行精确控制。更具体地说。。我想在我的工具栏中显示一个可编辑的项目列表

我有一个
toolbarItems
,最初是用一些
uibarbuttonims
设置的。我试图实现的效果是通过编程扩展工具栏的高度,然后在其余按钮的顶部显示
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领域,苹果不会。想法


你真正的问题是什么?你的行为是否合法?我使用了一些类似的技巧从uibarbuttonite到它所代表的视图,这从来都不是一个问题

例如,我使用下面的代码片段,没有任何问题。从技术上讲,这并不是根据so使用私有API,而是依赖于未记录的视图结构,这里还有部分类名,所以您应该知道自己在做什么。另外,请提交一份雷达文件,说明uibarbuttoneim混乱不堪,错过了一个明显的标志,无法进入实际视图

static UIView *PSToolbarViewForBarButtonItem(UIToolbar *toolbar, UIBarButtonItem *barButtonItem) {
    UIView *barButtonView = nil;
    for (UIControl *subview in toolbar.subviews) {
        if ([NSStringFromClass([subview class]) hasPrefix:@"UIToolbarB"] && [subview isKindOfClass:[UIControl class]]) {
            for (UIBarButtonItem *aBarButtonItem in [subview allTargets]) {
                if (barButtonItem == aBarButtonItem) { barButtonView = subview; break; }
            }
        }
    }
    return barButtonView;
}

此外,如果您这样做,那么编写的代码将在由于任何原因无法找到工具栏的视图时正常失败。我知道一些应用程序符合我的路线,而其他许多应用程序甚至不费事,只需编写自己的代码来创建工具栏。

我的具体问题是,我想在工具栏中显示一个可编辑的项目列表。。还有什么是“praxis”?您的解决方案的问题是,即使我可以根据自己的喜好调整UILabel的属性。。其他按钮仍然会被压扁b/c他们假设有第四个按钮。。我将看一下创建我自己的工具栏选项。a好的,只需手动在工具栏上添加一个视图并管理视图。但实际上,使用UIToolbar时,您要做的是非常困难的。这不是最灵活的类。