0

I have this UICollectionViewController class code :

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]))

      }}

My problem is that when I execute this code I get this :

http://postimg.org/image/jqwjt0o2f/

as you can see by the image , the two images are too small respect the UIImageview 's size , and I want that for example if I have the UIImageView' size is 30x30 the image fills all imageView.

I select color for two ImageView for show you the real size of this UIImageView

How do I solve this problem?

beyowulf
  • 15,101
  • 2
  • 34
  • 40
gianni
  • 113
  • 2
  • 9
  • I dont if I understand your problem. Do you simply want to change the content mode so that the image is scaled up? If yes, check the content mode property –  Apr 19 '16 at 22:29
  • My problem is that : if you see the image you can note that , for example , the first image ( in the uiimageview with green background) doesn't fill the entire uiimaview – gianni Apr 19 '16 at 22:34
  • I want that the image adapts to the size of uiimageview – gianni Apr 19 '16 at 22:36
  • Check https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/contentMode and set it to scaletofill –  Apr 19 '16 at 22:38
  • If i set scaleaspectfill the first image becomes : http://postimg.org/image/628ddq09j/ – gianni Apr 19 '16 at 22:41
  • it's too big respect the uiimageview – gianni Apr 19 '16 at 22:41
  • Set clips sub views to true. See here http://stackoverflow.com/questions/6638623/uiviewcontentmodescaleaspectfill-not-clipping – beyowulf Apr 19 '16 at 23:01
  • with clips sub set to true I lost part of image : http://postimg.org/image/a2yefudp3/ – gianni Apr 19 '16 at 23:04
  • You need to decide what you want. Do you want it stretched and the wrong aspect ratio? Just play around with the content mode. Obviously if the dimensions of the imageView are a different aspect ratio to the image you either need to change the imageviews dimensions or accept some clipping or accept stretching. – beyowulf Apr 19 '16 at 23:08
  • for understand better what I want I do an example : in real application , like Facebook , regardless of the size of the images that I load in my dashboard always they have the same size as the image adapts to the size of UIImageView – gianni Apr 19 '16 at 23:20

1 Answers1

0

If they are only two modes that can fit what you want. Fill and Scale (or resize) mode.

Fill Mode : Keep your image but can cut some part of your image. Resize or Scale Mode : Will resize your image to fit the image view