带有图像和标题的iOS 7 BarButtonItem [英] iOS 7 BarButtonItem with image and title

查看:96
本文介绍了带有图像和标题的iOS 7 BarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何实现这样的目标:





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

  UIBarButtonItem * customBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@Buttonstyle:UIBarButtonItemStyleBordered target:nil action:nil]; 

编辑



<这是我用以下代码实现的:

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



这里有两个问题:第一个是 UIButton 的大小,第二个是,正如你从gif中看到的那样,当我点击按钮内部并且当我释放水龙头时,按钮没有正确的动画。任何想法?

解决方案

您可以在UIBarButtonItem中嵌入UIButton作为自定义视图。您可以使用 -setImage:forState: -setTitle:forState:

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

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



有关详细信息,请参阅 initWithCustomView的文档:



你也可以做所有只需将UIButton拖动到导航栏上的左侧栏按钮/右侧栏按钮插槽,即可在界面构建器中进行此操作。


I'm trying to figure out how can I achieve something like this:

This is a toolbar and I'd like to keep the button title text without having to create the whole image with icon AND text. How can I add the icon to the left of the UIBarButtonItem while keeping the text set with:

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

EDIT

This is what I achieved with the following code:

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

Two issues here: the first is the size of the UIButton and the second is that, as you can see from the gif, the button doesn't animate properly when I tap inside the button and when I release the tap. Any ideas?

解决方案

You can embed a UIButton as a custom view inside your UIBarButtonItem. You can create your UIButton however you want, including with an image and text, using -setImage:forState: and -setTitle:forState:.

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

Note that when doing this, you'll need to attach your IBAction methods to customButton, not to customBarButtonItem.

For more info, see the documentation for initWithCustomView:.

You can also do all of this in interface builder just by dragging a UIButton to the left bar button/right bar button slot on a navigation bar.

这篇关于带有图像和标题的iOS 7 BarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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