【问题标题】:controlling display coordinates of view item added to toolbar控制添加到工具栏的视图项的显示坐标
【发布时间】: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


    【解决方案1】:

    您的实际问题是什么?如果您正在做的事情是否合法?我使用一些类似的技巧从 UIBarButtonItem 获取到由它表示的视图,这从来都不是问题。

    例如,我使用以下 sn-p 没有任何问题。从技术上讲,这并不是使用私有 API,而是依赖于未记录的视图结构以及这里的类名的一部分,所以你真的应该知道你在做什么。还请提交一份雷达报告,说明 UIBarButtonItem 被搞砸了,错过了一个明显的标志,无法到达实际视图。

    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;
    }
    

    另外,如果你走这条路,编写代码,如果由于任何原因无法找到工具栏的视图,该代码将正常失败。我知道一些应用程序符合我的要求,而许多其他应用程序甚至都不费心,只需编写自己的代码来创建工具栏。

    【讨论】:

    • 我的具体问题是我想在我的工具栏中的项目上显示一个 UILable .. 还有什么是“实践”?
    • 您的解决方案的问题是,即使我可以根据自己的喜好调整 UILabel 的属性.. 其他按钮仍然会被压扁 b/c 他们假设有第四个按钮。 . 我要看看创建我自己的工具栏选项..
    • Abood,只需手动将视图添加到工具栏并管理视图。但实际上,使用 UIToolbar 很难做到。这不是最灵活的类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2017-11-25
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多