1、自定义导航栏按钮
系统自带的UIBarButtonItem,图片和文字好像不能同时添加(我没找到同时添加的方法)。我在工程中遇到了需要背景图的导航栏右侧按钮,查了写资料之后还是放弃使用UIBarButtonItem。自定义了UIButton,添加到导航栏上。
//自定义搜索按钮
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(11, 8, 44, 24);
[button setTitle:@"搜索" forState:UIControlStateNormal];
[button setTitleColor:UIColorFromRGBOne(0xff) forState:UIControlStateNormal];
UIImage *image = [UIImage imageNamed:@"btn_zi.png"];
image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
UIImage *image1 = [UIImage imageNamed:@"btn_zi_h.png"];
image1 = [image1 stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image1 forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.font = GetFont(BFONT_12);
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barButton;
2、图片从中间拉伸
这其中用到的背景图,因为美工给的图片比较小,并且带有圆角,只能靠拉伸来填充整个UIButton。拉伸的话从图片中间进行拉伸,代码如下。
UIImage *image = [UIImage imageNamed:@"btn_zi.png"];
image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
UIImage *image1 = [UIImage imageNamed:@"btn_zi_h.png"];
image1 = [image1 stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image1 forState:UIControlStateHighlighted];
这样导航栏的按钮设置就完成了,
button.png,尺寸为:24x60
现在我们把它用作为按钮背景,按钮尺寸是150x50:
// 得到view的尺寸
CGSize viewSize = self.view.bounds.size;
// 初始化按钮
UIButton *button = [[UIButton alloc] init];
// 设置尺寸
button.bounds = CGRectMake(0, 0, 150, 50);
// 设置位置
button.center = CGPointMake(viewSize.width * 0.5f, viewSize.height * 0.5f);
// 加载图片
UIImage *image = [UIImage imageNamed:@"button"];
// 设置背景图片
[button setBackgroundImage:image forState:UIControlStateNormal];
// 添加按钮
[self.view addSubview:button];
原图:
button效果图:
可以看到,效果非常地差。原因很简单,因为原图大小为24x60,现在整张图片被全方位拉伸为150x50,比较严重的是图片的4个角。
有些人可能马上想到一个解决方案,你叫美工把图片做大一点不就好了么,怎么拉伸都没事。没错,这是一种解决方案,不过不建议采取。原因很简单:1.图片大,导致安装包也大,加载到内存中也大;2.有更好的解决方案。
细看一下图片,其实图片会变得难看,完全是因为4个角被拉伸了,中间的拉伸并没有明显地丑化外观。因此要想小图片被拉伸后不会变得难看,在图片拉伸的时候,我们只需拉伸图片的中间一块矩形区域即可,不要拉伸边缘部分。
比如只拉伸下图的矩形区域,上下左右的边缘都不拉伸:
iOS中提供很好用的API帮我们实现上述功能。到iOS 6.0为止,iOS提供了3种图片拉伸的解决方案,接下来分别详细介绍这些方案。
一、iOS 5.0之前
iOS中有个叫端盖(end cap)的概念,用来指定图片中的哪一部分不用拉伸。比如下图中,黑色代表需要被拉伸的矩形区域,上下左右不需要被拉伸的边缘就称为端盖。
使用UIImage的这个方法,可以通过设置端盖宽度返回一个经过拉伸处理的UIImage对象
二 、iOS 6.0
在iOS6.0中,UIImage又提供了一个方法处理图片拉伸
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
对比iOS5.0中的方法,只多了一个UIImageResizingMode参数,用来指定拉伸的模式:
UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
CGFloat top = 25; // 顶端盖高度
CGFloat bottom = 25 ; // 底端盖高度
CGFloat left = 10; // 左端盖宽度
CGFloat right = 10; // 右端盖宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 指定为拉伸模式,伸缩后重新赋值
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];