带有图像和标题的iOS 7 UIBarButtonItem

时间:2013-10-05 16:40:14

标签: objective-c ios7 uibarbuttonitem uitoolbar

我试图找出如何实现这样的目标:

enter image description here

这是一个工具栏,我想保留按钮标题文字,而不必使用图标和文字创建整个图像。如何在UIBarButtonItem左侧添加图标,同时保持文本设置为:

UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:nil action:nil];

修改

这是我通过以下代码实现的:

    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton setImage:[UIImage imageNamed:@"Test"] forState:UIControlStateNormal];
    [customButton setTitle:@"Button" forState:UIControlStateNormal];
    customButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [customButton sizeToFit];
    UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];

enter image description here

这里有两个问题:第一个是UIButton的大小,第二个是,从gif中可以看出,当我点击按钮内部时,按钮没有正常动画我释放了水龙头。有什么想法吗?

4 个答案:

答案 0 :(得分:31)

您可以在UIBarButtonItem中嵌入UIButton作为自定义视图。您可以使用-setImage:forState:-setTitle:forState:创建您想要的UIButton,包括图片和文字。

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:@"image"] forState:UIControlStateNormal];
[customButton setTitle:@"Button" forState:UIControlStateNormal];
[customButton sizeToFit];
UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.leftBarButtonItem = customBarButtonItem; // or self.navigationItem.rightBarButtonItem

请注意,执行此操作时,您需要将IBAction方法附加到customButton,而不是customBarButtonItem

有关详细信息,请参阅the documentation for initWithCustomView:

您也可以通过将UIButton拖动到导航栏上的左侧栏按钮/右侧栏按钮插槽,在界面构建器中完成所有这些操作。

答案 1 :(得分:4)

我对堆栈溢出时发现的所有解决方案都不满意,因为我喜欢UIBarButtonItenm如何转换图像,以便正确显示它们,并且可以使用淡色更改它们。所以我发现这个解决方案,我可以很好地使用没有太多代码...最终效果类似于Facebook和其他当前应用程序中的工具栏......

enter image description here

当您更改UIBArButtonItem中包含的视图的色调颜色时,图像和标题的颜色会相应变化,真的很酷,我根本不需要创建特殊图像。

此代码从每个按钮调用,如竞争按钮,并且按钮也设置为引用插座...

- (void)selectButton:(UIButton *)sender { 
    _competitionButton.superview.tintColor = [UIColor lightGrayColor];
    _usersButton.superview.tintColor = [UIColor lightGrayColor]; 
    _managementButton.superview.tintColor = [UIColor lightGrayColor];    
    sender.superview.tintColor = [UIColor whiteColor]; 
}

- (IBAction)onCompetitionButtonClick:(UIButton *)sender {
    [self selectButton:sender];
    [_scrollView scrollToPage:0 of:3];
}

在按钮中,标题为比赛只有标题,在栏按钮项目中有图像......这就是我设置它的方式并且它有效,也许还有其他可能性..

答案 2 :(得分:1)

所以我设法以编程方式完成我的解决方案...一些抱怨它的用户不起作用......那么如何使用图像和项目创建UTToolbar,以便您可以通过色调更改按钮的颜色,它会神奇地将图像更改为标题标签?我的UI编辑器解决方案确实有效... 在这段代码中可以更明确地澄清事情,你可以以编程方式做到......

- (void)loadToolbar {
    NSMutableArray *items = NSMutableArray.new;
    [items add:[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]];
    for (uint pageIndex = 0; pageIndex < _controllers.count; ++pageIndex) {
        UIView *itemView = [UIView.alloc initWithFrame:CGRectMake(0, 0, 100, 44)];
        itemView.backgroundColor = [UIColor clearColor];
        itemView.tintColor = [UIColor orangeColor];
        [itemView addSubview:[self createToolbarForItemView:pageIndex]];
        [itemView addSubview:[self createButtonForItemView:pageIndex]];
        UIBarButtonItem *item = [UIBarButtonItem.alloc initWithCustomView:itemView];
        item.style = UIBarButtonItemStylePlain;
        [items add:item];
        [items add:[UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]];
    }
    [_toolbar setItems:items];
}

- (UIButton *)createButtonForItemView:(uint)pageIndex {
    UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeSystem];
    itemButton.frame = CGRectMake(0, 0, 100, 44);
    itemButton.titleLabel.font = [UIFont systemFontOfSize:11];
    itemButton.contentEdgeInsets = UIEdgeInsetsMake(30, 0, 0, 0);
    itemButton.text = [_controllers[pageIndex] tabTitle];
    itemButton.touchUp = ^(UIButton *sender) {
        for (UIButton *button in _buttons) button.superview.tintColor = UI.toolBarInactiveButtonTextColor;
        sender.superview.tintColor = UI.toolBarActiveButtonTextColor;
        [self showPage:pageIndex];
    };
    [_buttons add:itemButton];
    return itemButton;
}

- (UIToolbar *)createToolbarForItemView:(uint)pageIndex {
    UIToolbar *itemToolbar = [UIToolbar.alloc initWithFrame:CGRectMake(0, 0, 100, 44)];
    itemToolbar.tintColor = nil;
    itemToolbar.barStyle = _toolbar.barStyle;
    itemToolbar.translucent = _toolbar.translucent;
    UIBarButtonItem *imageItem = [_controllers[pageIndex] tabImageItem];
    imageItem.style = UIBarButtonItemStylePlain;
    imageItem.tintColor = nil;
    imageItem.imageInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
    itemToolbar.items = @[
            [UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil],
            imageItem,
            [UIBarButtonItem createWithItem:UIBarButtonSystemItemFlexibleSpace :nil :nil]
    ];
    return itemToolbar;
}

答案 3 :(得分:0)

我解决了这个问题如下:

[Button **setTitleColor**:[UIColor colorWithRed:129/255.0f green:124/255.0f blue:110/255.0f alpha:1.0f] forState:**UIControlStateHighlighted**];