UIImageView和ScrollView奇怪的行为

时间:2015-09-02 21:18:41

标签: ios swift uitableview

我试图在titleView中使用PageControl创建一个imageView Carousel。现在,除了图像的对齐之外,一切似乎都有效。我在这做错了什么?看看它似乎不适合它的底部图像?

细胞

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
    }





}

viewController中的CellForRowAtIndexPath

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

1 个答案:

答案 0 :(得分:1)

您必须将UIImageView的{​​{3}}属性设置为true,此属性确定子视图是否仅限于视图的边界。您还需要设置clipsToBounds以指定视图在其大小更改为您想要的内容时如何调整其内容,如下所示:

newImage.contentMode = .ScaleAspectFill
newImage.clipsToBounds = true

我希望这对你有所帮助。