Objective c iOS 7带图像和标题的BarButtonItem

Objective c iOS 7带图像和标题的BarButtonItem,objective-c,ios7,uibarbuttonitem,uitoolbar,Objective C,Ios7,Uibarbuttonitem,Uitoolbar,我在想我怎样才能做到这一点: 这是一个工具栏,我想保留按钮标题文本,而不必创建带有图标和文本的整个图像。如何将图标添加到UIBarButtonItem左侧,同时将文本设置为: UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:nil action:nil]; 编辑

我在想我怎样才能做到这一点:

这是一个工具栏,我想保留按钮标题文本,而不必创建带有图标和文本的整个图像。如何将图标添加到
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];


这里有两个问题:第一个是
UIButton
的大小,第二个问题是,正如您从gif中看到的,当我在按钮内部轻按和松开轻按时,按钮无法正确设置动画。有什么想法吗?

您可以将UIButton作为自定义视图嵌入到UIBarButtonItem中。您可以使用和以任何方式创建UIButton,包括图像和文本

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

有关详细信息,请参阅


您也可以在interface builder中通过将UIButton拖动到导航栏上的左栏按钮/右栏按钮槽来完成所有这些操作。

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

当你改变了uibarbuttonite中包含的视图的颜色,图像的颜色和标题也相应地改变,真的很酷,我根本不需要创建特殊的图像

此代码从每个按钮调用,就像竞争按钮一样,并且按钮设置为引用出口

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

在有标题的按钮比赛中,只有标题,而在酒吧按钮项目中,有图像。。。这就是我设置它的方式,可能还有其他的可能性。

我解决了这个问题,如下所示:

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

所以我设法用编程的方式来解决问题。。。一些用户抱怨它不起作用。。。那个么,如何创建带有图像和项目的Uttolbar,这样你们就可以通过着色来改变按钮的颜色,从而神奇地改变图像和标题标签呢?我的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;
}
-(无效)加载工具栏{
NSMUTABLEARRY*items=NSMUTABLEARRY.new;
[项目添加:[UIBarButtonItem createWithItem:uiBarButtonSystemFlexibleSpace:nil:nil]];
对于(uint pageIndex=0;pageIndex<\u controllers.count;++pageIndex){
UIView*itemView=[UIView.alloc initWithFrame:CGRectMake(0,01000,44)];
itemView.backgroundColor=[UIColor clearColor];
itemView.tintColor=[UIColor orangeColor];
[itemView addSubview:[self-createToolbarForItemView:pageIndex]];
[itemView addSubview:[self-CreateButtonFormView:pageIndex];
UIBarButtonItem*item=[UIBarButtonItem.alloc initWithCustomView:itemView];
item.style=UIBarButtonimStyleplain;
[项目添加:项目];
[项目添加:[UIBarButtonItem createWithItem:uiBarButtonSystemFlexibleSpace:nil:nil]];
}
[_工具栏设置项:项];
}
-(UIButton*)CreateButtonFormView:(uint)页面索引{
UIButton*itemButton=[UIButton按钮类型:UIButtonTypeSystem];
itemButton.frame=CGRectMake(0,0,100,44);
itemButton.titleLabel.font=[UIFont systemFontOfSize:11];
itemButton.contentEdgeInsets=UIEdgeInsetsMake(30,0,0,0);
itemButton.text=[[u控制器[pageIndex]选项卡标题];
itemButton.touchUp=^(UIButton*发送者){
对于(UIButton*按钮中的按钮)button.superview.tintColor=UI.toolbarnactivebuttonextcolor;
sender.superview.tintColor=UI.toolbaractivebuttonextcolor;
[自显示页面:页面索引];
};
[_按钮添加:itemButton];
返回项目按钮;
}
-(UIToolbar*)createToolbarForItemView:(uint)页面索引{
UIToolbar*itemToolbar=[UIToolbar.alloc initWithFrame:CGRectMake(0,0,100,44)];
itemToolbar.tintColor=nil;
itemToolbar.barStyle=\u toolbar.barStyle;
itemToolbar.transparent=\u toolbar.transparent;
UIBarButtonItem*imageItem=[\u控制器[pageIndex]选项卡imageItem];
imageItem.style=uiBarButtonimStyleplain;
imageItem.tintColor=nil;
imageItem.imageInsets=UIEdgeInsetsMake(-10,0,0,0);
itemToolbar.items=@[
[UIBarButtonItem createWithItem:UIBarButtonSystemFlexibleSpace:nil:nil],
imageItem,
[UIBarButtonItem createWithItem:UIBarButtonSystemFlexibleSpace:nil:nil]
];
返回项目工具栏;
}

你不能只使用ASCII“向上箭头”字符?@Undo,这只是一个示例图标。如果我在
UIBarButtonItem
中嵌入
UIButton
,我需要设置
UIButton
的框架,我不会这样做,因为我使用了自动布局。除此之外,如果我使用这种方式,当我点击按钮时,按钮不会“褪色”(动画)。@FredCollins,当你点击按钮时,按钮应该会褪色(我刚刚测试过)。你必须为州等设置标题颜色,就像其他按钮一样。它会淡出文本,但不会淡出图标图像。它对你有用吗?我添加了一个示例,说明如何创建自定义按钮而无需显式设置框架。是的,它会使图标图像褪色。我用我正在使用的代码更新了我的答案。人们可能是盲人。。。对我来说仍然是非常有用的解决方案,我只是考虑以编程方式做……我不得不承认,我有一段很难的时间来弄明白如何用直代码翻译这个故事板解决方案。我只需要代码。。。(为了记录在案,我没有否决你的答案。)到目前为止,我找到的其他解决方案也没有让我满意。创建一个既有图标又有标签的uibarbuttonite是多么的困难,真是不可思议
- (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;
}