在某些情况下,我们使用的UIButton的背景图片不一定就是标准的尺寸,有时会偏大,那么怎么办?

这个比较简单直接设置 :    self.imageView.contentMode = UIViewContentModeScaleAspectFill;

但是问题来了,如果图片尺寸比较button的bounds 小的话,他不会撑满整个控件默认会居中显示,有些时候这并不是我们想要的效果。有时我们希望他能撑满整个button。

1
- (CGRect)imageRectForContentRect:(CGRect)bounds{
2
        return CGRectMake(0.0, 0.0, self.size.width, self.size.height);
3
}

那么这样处理即可:

继承UIButton,重写 imageRectForContentRect 方法返回button的bounds

btn.imageView setContentMode:UIViewContentModeScaleAspectFill];

下面方法自己可以试下, 你就知效果了...

typedef NS_ENUM(NSInteger, UIViewContentMode) {

UIViewContentModeScaleToFill,

UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent内容部分符合固定方面,其余内容是透明的

UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.内容扩充固定的方面,部分内容可能被剪

UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)范围变化重绘

UIViewContentModeCenter,              // contents remain same size. positioned adjusted.内容保持相同大小定位调整

UIViewContentModeTop,//贴顶

UIViewContentModeBottom,//贴底

UIViewContentModeLeft,//靠左

UIViewContentModeRight,//靠右

UIViewContentModeTopLeft,//左上

UIViewContentModeTopRight,//右上

UIViewContentModeBottomLeft,//左下

UIViewContentModeBottomRight,//右下

};

UIButton设置imgae图片自适应button的大小且不变形的更多相关文章

  1. css设置背景图片自适应

      CreateTime--2017年12月25日16:36:07 Author:Marydon 控制背景图片100%自适应填充布局 /* 控制背景图片100%自适应填充布局 */ body{ bac ...

  2. bootstrop设置背景图片自适应屏幕

    如果不用bootstrop框架,想让背景图片自适应窗口大小,可以这样做: <style type="text/css"> html{height: %;} body.a ...

  3. android设置图片自适应控件大小

    在XML文件的ImageView属性中加上:android:scaleType="fitXY"

  4. 在JLabel上显示图片,并且图片自适应JLabel的大小

    本文转载地址:       http://blog.csdn.net/xiaoliangmeiny/article/details/7060250 在写<Core Java>上的示例代码时 ...

  5. 使div下的图片自适应div的大小

    div img{ max-width:100%; height:auto; } 这里div 要给固定的宽度 开始这里还想了半天 用网上的方法也不行 问老大 又一句话就给我解决了...老大真男神啊!!! ...

  6. CSS 图片自适应容器

    https://www.jb51.net/css/660677.html 经常有这样一个场景,需要让图片自适应容器的大小. 1.img标签的方式 我们马上就能想到,把width.height 设置为1 ...

  7. css控制图片自适应大小

    相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决.      该CSS的功能是:大于600的图片自动调整为600显示. <style ...

  8. background-size (设置背景图片的大小)

    设置背景图片的大小,以长度值或百分比显示(数值包括 长度length和百分比percentage),还可以通过cover和contain来对图片进行伸缩. 语法:background-size: au ...

  9. Button 设置适应不同版本 旋转以后大小相应的改变

    很多时候对于不同的版本,随设备的旋转以后,相应的Button的大小如果不做相应的改变,这很影响视图的美观和布局:下面是小编的个人看法  UIButton *button = [[UIBtton all ...

随机推荐

  1. 帝国CMS商城功能高级使用

    一.常见问题1.为什么自定义模型加入购物车提示“非商城表的信息”?答:模型要使用商城的功能,要先到:后台>商城>商城参数设置:“指定使用商肠能的数据表”勾选你这个自定义模型表,才可以使用. ...

  2. 学习git遇到的一些简单错误

    From:http://stackoverflow.com/questions/7574459/prompted-for-password-on-git-pull-origin-branch [roo ...

  3. SSH学习笔记目录

    1.Spring_2000_Spring_Hibernate_HibernateTemplate

  4. linux下zip文件解压后乱码解决方案

    解决办法一,利用pyton来处理 1.vi uzip文件2.复制一下内容(Python) #!/usr/bin/env python # -*- coding: utf-8 -*- # uzip.py ...

  5. scrapy 模拟登录后再抓取

    深度好文: from scrapy.contrib.spiders.init import InitSpider from scrapy.http import Request, FormReques ...

  6. [经验总结]利用xlstproc处理XSLT的makefile

    转自:http://blog.csdn.net/thinkhy/article/details/5343739 # For XSLT PARSE = xsltproc SRC = main.xml S ...

  7. CentOS 安装 Chrome

    cd  /etc/yum.repos.d/ vi  google.repo [gogle] name=Google-x86_64 baseurl=http://dl.google.com/linux/ ...

  8. [ActionScript3.0] 为内建类添加方法

    通过使用prototype在继承内建类特性的同时加入新方法 Array.prototype.removeElement = function (item:*):void { var index:int ...

  9. zkclient

    zkclient 快速指南 Maven依赖 最新的版本发布在Maven中央库. <dependency> <groupId>com.github.adyliu</grou ...

  10. 用Java开发gRPC服务的例子分析

    本文的代码例子来自:https://github.com/grpc/grpc-java  定义服务 这一步与其他语言完全一样,需要定义gRPC的服务.方法.request和response的类型. 完 ...