UIViewContentMode 各个属性及效果

api

public enum UIViewContentMode : Int {    
    case ScaleToFill
    case ScaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
    case ScaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    case Redraw // redraw on bounds change (calls -setNeedsDisplay)
    case Center // contents remain same size. positioned adjusted.
    case Top
    case Bottom
    case Left
    case Right
    case TopLeft
    case TopRight
    case BottomLeft
    case BottomRight
}

使用方法

UIImageView.contentMode  = UIViewContentModel.*

效果图

110854_Bcpv_2547410.jpeg

我的demo如下:

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        var w:CGFloat = self.view.frame.size.width
        var h:CGFloat = self.view.frame.size.height
        var y:CGFloat=10
        
        //初始化组件
        let scrollview = UIScrollView()
        scrollview.frame = CGRectMake(10, y, w - 20 , h - 20 )
        scrollview.layer.borderWidth=1
        scrollview.scrollsToTop = true
        scrollview.scrollEnabled = true
        scrollview.pagingEnabled = false
        scrollview.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(scrollview)
        
        y += 10
        w /= 2
        h = w*1.5
        
        let ScaleToFill = UIImageView(frame: CGRectMake(20, y , w, h))
        ScaleToFill.layer.borderWidth = 1
        ScaleToFill.image = UIImage(named: "img.png")
        ScaleToFill.contentMode = UIViewContentMode.ScaleToFill;//拉伸
        scrollview.addSubview(ScaleToFill)
        
        y += h + 30
        let Redraw = UIImageView(frame: CGRectMake(20, y , w, h))
        Redraw.layer.borderWidth = 1
        Redraw.image = UIImage(named: "img.png")
        Redraw.contentMode = UIViewContentMode.Redraw;//拉伸
        scrollview.addSubview(Redraw)
        
        y += h + 30
        let ScaleAspectFit = UIImageView(frame: CGRectMake(20, y , w, h))
        ScaleAspectFit.layer.borderWidth = 1
        ScaleAspectFit.image = UIImage(named: "img.png")
        ScaleAspectFit.contentMode = UIViewContentMode.ScaleAspectFit;//压缩比例完全显示
        scrollview.addSubview(ScaleAspectFit)
        
        y += h + 30
        let ScaleAspectFill = UIImageView(frame: CGRectMake(20, y , w, h))
        ScaleAspectFill.layer.borderWidth = 1
        ScaleAspectFill.image = UIImage(named: "img.png")
        ScaleAspectFill.contentMode = UIViewContentMode.ScaleAspectFill;//高度或者宽度完全显示
        scrollview.addSubview(ScaleAspectFill)
        
        y += h + 30
        let Center = UIImageView(frame: CGRectMake(20, y , w, h))
        Center.layer.borderWidth = 1
        Center.image = UIImage(named: "img.png")
        Center.contentMode = UIViewContentMode.Center;
        scrollview.addSubview(Center)
        
        y += h + 30
        let Top = UIImageView(frame: CGRectMake(20, y , w, h))
        Top.layer.borderWidth = 1
        Top.image = UIImage(named: "img.png")
        Top.contentMode = UIViewContentMode.Top;
        scrollview.addSubview(Top)
        
        y += h + 30
        let Bottom = UIImageView(frame: CGRectMake(20, y , w, h))
        Bottom.layer.borderWidth = 1
        Bottom.image = UIImage(named: "img.png")
        Bottom.contentMode = UIViewContentMode.Bottom;
        scrollview.addSubview(Bottom)
        
        y += h + 30
        let Left = UIImageView(frame: CGRectMake(20, y , w, h))
        Left.layer.borderWidth = 1
        Left.image = UIImage(named: "img.png")
        Left.contentMode = UIViewContentMode.Left;
        scrollview.addSubview(Left)
        
        y += h + 30
        let Right = UIImageView(frame: CGRectMake(20, y , w, h))
        Right.layer.borderWidth = 1
        Right.image = UIImage(named: "img.png")
        Right.contentMode = UIViewContentMode.Right;
        scrollview.addSubview(Right)
        
        y += h + 30
        let TopLeft = UIImageView(frame: CGRectMake(20, y , w, h))
        TopLeft.layer.borderWidth = 1
        TopLeft.image = UIImage(named: "img.png")
        TopLeft.contentMode = UIViewContentMode.TopLeft;
        scrollview.addSubview(TopLeft)
        
        y += h + 30
        let TopRight = UIImageView(frame: CGRectMake(20, y , w, h))
        TopRight.layer.borderWidth = 1
        TopRight.image = UIImage(named: "img.png")
        TopRight.contentMode = UIViewContentMode.TopRight;
        scrollview.addSubview(TopRight)
        
        y += h + 30
        let BottomLeft = UIImageView(frame: CGRectMake(20, y , w, h))
        BottomLeft.layer.borderWidth = 1
        BottomLeft.image = UIImage(named: "img.png")
        BottomLeft.contentMode = UIViewContentMode.BottomLeft;
        scrollview.addSubview(BottomLeft)
        
        y += h + 30
        let BottomRight = UIImageView(frame: CGRectMake(20, y , w, h))
        BottomRight.layer.borderWidth = 1
        BottomRight.image = UIImage(named: "img.png")
        BottomRight.contentMode = UIViewContentMode.BottomRight;
        scrollview.addSubview(BottomRight)
        
        y+=100
        scrollview.contentSize = CGSizeMake(self.view.frame.size.width, y)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


转载于:https://my.oschina.net/asjoker/blog/543793

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值