zoukankan      html  css  js  c++  java
  • UIButton设置imgae图片自适应button的大小且不变形

    在某些情况下,我们使用的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,//右下

    };

  • 相关阅读:
    [luogu4799 CEOI2015 Day2] 世界冰球锦标赛(折半搜索)
    [luogu3230 HNOI2013] 比赛 (搜索+Hash)
    [luogu2317 HNOI2005] 星际贸易 (dp)
    [luogu2414 NOI2011]阿狸的打字机 (AC自动机)
    [bzoj3507 Cqoi2014]通配符匹配 (hash+DP)
    [luogu2054 AHOI2005] 洗牌 (数论)
    bzoj1491 [NOI2007]社交网络
    bzoj1022 [SHOI2008]小约翰的游戏John
    bzoj1088 [SCOI2005]扫雷Mine
    bzoj1295 [SCOI2009]最长距离
  • 原文地址:https://www.cnblogs.com/OIMM/p/5576853.html
Copyright © 2011-2022 走看看