博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS图像拉伸解决方案
阅读量:7003 次
发布时间:2019-06-27

本文共 2153 字,大约阅读时间需要 7 分钟。

UIButton实现背景拉伸,即图片两端不拉伸中间拉伸的办法有如下两种:

第一种方法很简单而且使用性更广。做法就是直接拉伸想要setBackgroundImage的image,代码如下: 

  1. UIImage *image = [UIImage imageNamed:@"image.png"];   
  2. image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];  

设置了左端帽之后,rightCapWidth = image.size.width - (image.leftCapWidth + 1); 也就是说图片中间的一像素用来拉伸。垂直方向同上。设置之后无论把image放到什么控件中都可以自动拉伸了。

  

  1. UIImage *buttonImage = [UIImage imageNamed:@"contact.png"];  
  2. buttonImage = [buttonImage stretchableImageWithLeftCapWidth:floorf(buttonImage.size.width/2) topCapHeight:floorf(buttonImage.size.height/2)];  
  3.   
  4. UIImage *buttonImageselected = [UIImage imageNamed:@"contactselected.png"];  
  5. buttonImage = [buttonImage stretchableImageWithLeftCapWidth:floorf(buttonImage.size.width/2) topCapHeight:floorf(buttonImage.size.height/2)];  
  6.   
  7. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
  8. button.frame = CGRectMake(0024044);  
  9. [button setBackgroundImage:buttonImage forState:UIControlStateNormal];  
  10. [button setBackgroundImage:buttonImageselected forState:UIControlStateHighlighted];  
  11. button.center = CGPointMake(160240);  
  12. [button setTitle:@"button" forState:UIControlStateNormal];  
  13. [button setTitle:@"buttonClick" forState:UIControlStateHighlighted];  
  14. [self.view addSubview:button];  

第二种方法是在UIButton中加入一个UIImageView,拉伸imageView,然后将button的背景设为clearColor等等。把imageView放入button中,并且sendToBack,得到效果。代码如下:
 //刚才imageView拉伸的代码  

  1. UIImageView *strechTest = [[UIImageyiView alloc] initWithImage:[UIImage imageNamed:@"contact.png"]];  
  2. [strechTest setContentStretch:CGRectMake(0.5f0.5f0.f0.f)];  
  3. CGRect frame = strechTest.frame;  
  4. frame.size.width += 100;  
  5. strechTest.frame = frame;  

 

  1. //把imageView放入button中,并设置为back  
  2.  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
  3.  button1.frame = frame;  
  4.  button1.center = CGPointMake(160140);  
  5.  [button1 addSubview:strechTest];  
  6.  [button1 sendSubviewToBack:strechTest];  
  7.  [button1 setBackgroundColor:[UIColor clearColor]];  
  8.  [button1 setTitle:@"button" forState:UIControlStateNormal];  
  9.  [button1 setTitle:@"buttonClick" forState:UIControlStateHighlighted];  
  10.  [self.view addSubview:button];  

效果:

 

转载于:https://www.cnblogs.com/ios8/p/ios-pic-lashen.html

你可能感兴趣的文章
因为链接服务器 "IP" 的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务
查看>>
Spring框架入门案例
查看>>
Macbook pro air 装双系统 win 7/8 64位 驱动 bootcamp
查看>>
如何让所有模块先检测是否有登录
查看>>
php编译安装 GD库与mysqli 与curl
查看>>
sqlserver 2008 修改表结构不能保存
查看>>
Linux集群理论及技术
查看>>
Linux安装光盘修复GRUB
查看>>
磁盘管理
查看>>
我的友情链接
查看>>
报错:R cannot be resolved to a variable
查看>>
Haproxy全透明代理的部署
查看>>
lock wait执行计划看索引的重要性
查看>>
【python】字符串、16进制等数据处理
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
rhel5.5快速搭建本地yum源
查看>>
Zabbix触发器支持的函数说明
查看>>
19岁***克利里给西方网络安全敲响警钟
查看>>
Java邮箱验证
查看>>