我正在尝试在titleView中创建一个具有PageControl的imageView轮播。除了图像对齐,一切似乎都可以正常工作了。我在这里做错了什么?看一下底部的图像,看起来似乎不适合全部?
单元格
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
}
最佳答案
您必须将clipsToBounds
的 UIImageView
属性设置为true
,此属性确定子视图是否限制在视图范围内。您还需要设置 contentMode
来指定视图大小更改为所需大小时如何调整其内容,如下所示:
newImage.contentMode = .ScaleAspectFill
newImage.clipsToBounds = true
希望对您有所帮助。
关于ios - UIImageView和ScrollView怪异的行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32363274/