控制添加到工具栏的视图项的显示坐标 [英] controlling display coordinates of view item added to toolbar

查看:51
本文介绍了控制添加到工具栏的视图项的显示坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想精确控制添加到UINavigationController工具栏中的自定义视图。更具体地说,..我想在工具栏中显示项目的UILable

I want to have precise control over the custom view I add to my UINavigationController toolbar. More specifically.. i want to display a UILable ontop of the items in my toolbar.

我有一个 toolbarItems 最初设置了一些 UIBarButtonItems 。我试图实现的效果是通过编程方式扩展了工具栏的高度,然后在其余按钮的顶部显示 UILabel 。.这就是我目前拥有的:

I have a toolbarItems initially set up with some UIBarButtonItems. The effect I'm trying to achieve is programmatically expand the height of the toolbar, and then display a UILabel ontop of the rest of the buttons.. this is what I currently have:

-(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领域,某事苹果不会不能容忍

the result of this is that all the existing buttons get squashed, and the label takes their place. The problem is that If I try to get a little too creative and start manually messing with the subviews of the toolbar, I start wandering into undocumented API land, something Apple won't tolerate. Ideas?

推荐答案

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

What's your actual question? If what you're doing is legal or not? I use some similar tricks to get from the UIBarButtonItem to the view that is represented from it, and it has never been a problem.

例如,我使用以下代码片段任何问题。从技术上讲,这并不是在使用私有API,而是依靠未公开的视图结构以及此处的部分类名,因此您真的应该知道您在做什么。还请提交雷达报告,告知UIBarButtonItem混乱,并且错过了一个明显的标记以进入实际视图。

For example, I use following snippet without any issues. Technically this isn't using private API per so, but relying on undocumented view structure and here also part of the class names, so you really should know what you're doing. Please also file a radar that UIBarButtonItem is messed up and misses an obvious flag to get to the actual view.

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

此外,如果按照这种方式编写代码,如果找不到任何原因的工具栏视图。我知道有些应用程序会走我的路,而其他许多应用程序甚至都不会打扰,只需编写自己的代码即可创建工具栏。

Also, if you go that route, write code that will fail gracefully if for any reason the view for the toolbar can't be found. I know some apps that go my route, while lots of others don't even bother and simply write their own code to create a toolbar.

这篇关于控制添加到工具栏的视图项的显示坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆