IOS:制作自定义栏按钮,但仍然需要IOS的边框6

IOS:制作自定义栏按钮,但仍然需要IOS的边框6

问题描述:

我正在开发一款应用程序,需要同时支持IOS 6和7.我已经创建了一个自定义按钮,它在IOS 7中看起来不错。但是,对于IOS 6,它缺少您在大多数按钮上看到的边框。我不得不使用UIButtonTypeCustom创建按钮,因为我需要一个背景图像和一些文字覆盖。有没有办法让IOS 6回到边界?这里是我的代码和它看起来像目前在IOS 6IOS:制作自定义栏按钮,但仍然需要IOS的边框6

UIImage *commentsImage = [UIImage imageNamed:@"commentIcon"]; 
UIButton *commentsButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

CGRect frame = commentsButton.frame; 
frame = CGRectMake(0, 0, 26, 27); 
commentsButton.frame = frame; 
[commentsButton setTitle:[NSString stringWithFormat:@"%d", self.story.commentCount] forState:UIControlStateNormal]; 
commentsButton.titleLabel.font = [UIFont systemFontOfSize:12]; 
[commentsButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 6, 0)]; 
[commentsButton setBackgroundImage:commentsImage forState:UIControlStateNormal]; 
[commentsButton addTarget:self action:@selector(comments) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *commentsBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:commentsButton]; 

Navigation bar

它看起来并不坏,但我想它是一个小更均匀的屏幕截图。

此刻,您将背景图像设置为您的自定义评论图像。

通常您会创建一个可调整大小的背景图像,模仿边框,并将其设置为背景图像。

在你的情况下,我会建议继承的UIButton并执行以下操作:

设置背景图片是一个可调整大小的边框图像。 将图像设置为评论图像。 添加您自己的UILabel并将文本设置为您需要的任意数字。

这就是我会做的。 对不起,我没有时间更深入,但也许别人可能会回答更多细节。

+0

听起来像一个体面的解决方案。我真的很希望不必为了边界制作背景图片。 –