Peter Pik
Peter Pik

Reputation: 11193

UIImageView and ScrollView weird behaviour

I'm trying to create a imageView Carousel with a PageControl in the titleView. Everything seem to work now, beside the alignment of the images. What am i doing wrong here? Look at the bottom image where it does not seem to fit it all?

Cell

class ImageViewCell: UITableViewCell, UIScrollViewDelegate {

    @IBOutlet var imageScroll:UIScrollView?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        //scrollView

        imageScroll!.backgroundColor = UIColor.blackColor()
        imageScroll!.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        imageScroll!.showsHorizontalScrollIndicator = false
        imageScroll!.showsVerticalScrollIndicator = false
        imageScroll!.pagingEnabled = true
        imageScroll!.contentMode = UIViewContentMode.Center
        imageScroll!.bounces = false


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }





}

CellForRowAtIndexPath in viewController

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as! ImageViewCell



    cell.imageScroll!.contentSize = CGSizeMake(self.view.frame.width * CGFloat(pageImages.count), 200)
    cell.imageScroll!.delegate = self
    for (index, element) in pageImages.enumerate() {

        let newImage = UIImageView(frame: CGRectMake(CGFloat(index) * self.view.frame.width + 4, 0, self.view.frame.width, 200))
        newImage.image = element
        cell.imageScroll!.addSubview(newImage)


    }


    return cell
}

enter image description here

Upvotes: 1

Views: 42

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23451

You have to set the clipsToBounds property of the UIImageView to true, this property determines whether subviews are confined to the bounds of the view. You need to set too the contentMode to specify how a view adjusts its content when its size changes to any you want, something like this:

newImage.contentMode = .ScaleAspectFill
newImage.clipsToBounds = true

I hope this help you.

Upvotes: 1

Related Questions