我有这个UICollectionViewController
类代码:
class ViewController1: UICollectionViewController , UICollectionViewDelegateFlowLayout{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
self.collectionView?.alwaysBounceVertical = true
collectionView?.registerClass(Cell.self, forCellWithReuseIdentifier: "cellid")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCellWithReuseIdentifier("cellid", forIndexPath: indexPath)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 100)
}
}
class Cell :UICollectionViewCell {
//var i : Int = 0
override init(frame: CGRect) {
super.init(frame: frame)
self.setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var namelabel : UILabel = {
let label = UILabel()
label.text = "test"
//label.font = UIFont.boldSystemFontOfSize(14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var distanzalabel : UILabel = {
let label = UILabel()
label.text = "200KM"
//label.font = UIFont.boldSystemFontOfSize(14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
var imageV : UIImageView = {
let imageView = UIImageView()
//imageView.frame = CGRectMake(0, 0, 450 , 150);
imageView.image = UIImage(named: "2.jpg")
imageView.contentMode = .ScaleAspectFit
//imageView.clipsToBounds = true
imageView.backgroundColor = UIColor.greenColor()
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
var imageV1 : UIImageView = {
let imageView = UIImageView()
imageView.frame = CGRectMake(0, 0, 20 , 20);
imageView.image = UIImage(named: "User Male Filled.png")
imageView.contentMode = .ScaleAspectFit
//imageView.clipsToBounds = true
imageView.backgroundColor = UIColor.blackColor()
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
func setupViews(){
backgroundColor = UIColor.whiteColor()
self.addSubview(namelabel)
self.addSubview(distanzalabel)
self.addSubview(imageV)
self.addSubview(imageV1)
let lunghezza = self.frame.width
let result = lunghezza - 100
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v0(\(lunghezza))]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageV]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(result)-[v0(30)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageV1]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[v0(33)]-4-[v1]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageV , "v1" : namelabel]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[v0(33)]-4-[v1]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : imageV , "v1" : imageV1]))
}}
我的问题是,当我执行此代码时,我得到了这个:
http://postimg.org/image/jqwjt0o2f/
从图像中可以看出,这两个图像对于UIImageview的大小来说太小了,我希望这样,例如,如果我有UIImageView
'大小为30x30,图像填充所有imageView。
我为两个ImageView选择颜色,以显示此UIImageView
如何解决这个问题?
答案 0 :(得分:0)
如果他们只有两种模式可以满足你的需要。 填充和缩放(或调整大小)模式。
填充模式:保留图像,但可以剪切图像的某些部分。 调整大小或缩放模式:将调整图像大小以适合图像视图