ios - UIImageView 和 ScrollView 奇怪的行为

标签 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

最佳答案

您必须设置 clipsToBounds UIImageView 的属性设置为 true,此属性决定 subview 是否限制在 View 的边界内。您还需要设置 contentMode指定 View 在其大小更改为您想要的任何大小时如何调整其内容,如下所示:

newImage.contentMode = .ScaleAspectFill
newImage.clipsToBounds = true

希望对你有帮助

关于ios - UIImageView 和 ScrollView 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363274/

相关文章:

iphone - iOS - 将 LaunchOptions 传递给子方法

swift - 在 TextView 之间切换时,不同的键盘高度会产生奇怪的框架

iphone - 刷新 didSelectRowAtIndexPath 委托(delegate)上的 viewForFooterInSection

iOS 应用程序在点击按钮将新行添加到 UITableView 时崩溃

ios - 具有属性的 NSMutableArray

ios - 在 Swift 中以编程方式使用 segues 传递信息

Swift 2 到 3 迁移 String.fromCString

ios - 委托(delegate)不能使用 present() 和 dismiss()

ios - 在没有 Storyboard的情况下使用 UISearchController

ios - 在 iOS 上将 font-awesome 与 Phonegap 结合使用