iPhone : making UIBarButtonItem that is arrow shaped

标签 iphone xcode uinavigationcontroller uibarbuttonitem

我在导航栏上有一个 UIBarButtonItem。我想把它做成箭头形状。我的意思是我希望它是方形的,除了尖边。有谁知道该怎么做吗?

谢谢!

最佳答案

我在这个问题上尝试了多种方法,并最终使用多个来源的所有答案找到了答案。有几个技巧;此代码片段将显示所有内容(我正在创建一个自定义 rightNavButton ,但您可以轻松地针对任何 UIBarButtonItem 进行调整):

// Produces a nice "right arrow" style button that mirrors the back arrow
// automatically added by the navController
//
UIImage *buttonImage = [UIImage imageNamed:@"forwardButton.png"];
UIButton *forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
[forwardButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[forwardButton setTitle:@"Meter" forState:UIControlStateNormal];
forwardButton.font = [UIFont boldSystemFontOfSize:12];
forwardButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[forwardButton addTarget:self action:@selector(showMeter) 
           forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
           initWithCustomView:forwardButton];

注意:事实证明,如果您在使用 UIButton 分配自定义 View 后尝试直接在按钮项上设置目标和操作,它不会采取 - 你必须在按钮本身上设置目标和操作 - 显然 navBar使用逐字提供的按钮。

这是我在不透明黑色上使用的图像 navBar (显然,你可以使用任何东西):http://raretiger.com/images/forwardbutton.png

关于iPhone : making UIBarButtonItem that is arrow shaped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2330157/

相关文章:

iphone - 任何自动布局大师能够启发我有关约束和旋转的知识吗?

cocoa - mime 和邮件框架? - cocoa

iphone - 如何在推送 UIViewController 之前预加载 UITableView?

iphone - 将应用程序二进制文件上传到 iTunes

ios - 调用暂停后 NSProgress 恢复

ios - 将数据从 iPhone 发布到服务器(解析数据)

ios - ScrollView 对齐问题

iphone - 转到应用程序中的第一个 View Controller

ios - 导航栏中的多个搜索栏

ios - 为什么UICollectionViewCell的outlet在Interface Builder Xcode7.1 OS X Yosemite中为nil?