UIBarbuttonItem在ios 7上没有标题

时间:2013-10-02 10:12:47

标签: ios ios7 uibarbuttonitem

我有一个应用,我正在迁移到iOS 7。 但是,UIBarbuttonItem没有标题,但正常工作。 这是我的代码:

UIBarButtonItem * uibbShare = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sharewhite.png"] style:UIBarButtonItemStylePlain target:self action:@selector(sharePressed:)] autorelease];
// uibbShare.width = 56.0;  // uncommenting this doesn't change anything
uibbShare.title = @"Share";

然后我将其中一些添加到工具栏中,其中包含一些灵活的空格项。

...
 [items addObject:flex2];
 [items addObject:uibbShare];
...

[self.toolbar setItems:items animated:NO];

在iOS 7上他们根本没有标题,在iOS6上一切正常。难道你不能再在ios7中创建这样的barbuttons吗?

更新开发论坛上的同样问题:

UIBarButtonItem can't have both title and image?

What happened to the text under toolbar icons?

修改 :( 7 vs 6)

enter image description here

编辑2 :(来自Reveal的图片,文字似乎消失了,帧/边界为0. wtf)

enter image description here

3 个答案:

答案 0 :(得分:1)

最终最终为UIBarButtonItems创建了自定义视图。这不好,但它暂时起作用。我只是将旧的UIBarButtonItems传递给这个返回一个新函数的函数。

注意:如果有标题并且在下面添加一个简单的标签,我只是向上移动按钮的图像,而不是实际上弄乱了titleEdgeInsets和居中。另外,我将宽度设置为默认值56。

-(UIBarButtonItem *)convertToButton:(UIBarButtonItem *)original
{
    if ( SYSTEM_VERSION_LESS_THAN(@"7.0"))
        return  original;

    CGFloat textFieldHeight = 13.f;
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 56, self.toolbar.frame.size.height);
    button.showsTouchWhenHighlighted = YES;
    [button setImage:original.image forState:UIControlStateNormal];
    [button addTarget:original.target action:original.action forControlEvents:UIControlEventTouchUpInside];
    button.tag = original.tag;

    UIView * wr = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 56, self.toolbar.frame.size.height)] autorelease];
    [wr addSubview:button];
    wr.backgroundColor = [UIColor clearColor];

    if (original.title != nil)
    {
        button.imageEdgeInsets = UIEdgeInsetsMake(-7, 0, 0, 0);
        UILabel * l = [[[UILabel alloc] initWithFrame:CGRectMake(0, self.toolbar.frame.size.height - textFieldHeight, 56, textFieldHeight)] autorelease];
        l.font = [UIFont systemFontOfSize:11.f];
        l.backgroundColor = [UIColor clearColor];
        l.textColor = [UIColor lightGrayColor];
        l.textAlignment = UITextAlignmentCenter;
        l.text = original.title;
        [wr addSubview:l];
    }

    UIBarButtonItem * theNew = [[[UIBarButtonItem alloc] initWithCustomView:wr] autorelease];
    theNew.tag = original.tag;
    theNew.width = 56;
    return theNew;

}

答案 1 :(得分:0)

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont 
    fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
    [UIColor whiteColor], NSForegroundColorAttributeName, nil];

[[UINavigationBar appearance] setTitleTextAttributes:attributes]; 关键是使用NSFontAttributeName等等。我假设他们正在转向使用NS品种进行64位兼容。以上代码适用于我的iOS7设备

答案 2 :(得分:0)

尝试修改按钮的外观:

[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor textColor], UITextAttributeTextColor, [UIFont yourFont and yourSizeSont], UITextAttributeFont, [UIColor shadowColor], UITextAttributeTextShadowColor, [NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset, nil] forState:UIControlStateNormal];

在我上一次评论后,我想到了这样的事情:

NSArray *views = [_UIToolbarNavigationButton subviews];

for (UIView *view in views) {
    if ([view isKindOfClass:[UIButtonLabel class]])
    {
        view.hidden = NO;
    }
}

我不知道它是否有用。