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.*
效果图
我的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.
}
}