您好我有一个以编程方式添加的UIScrollView然后在其中添加UIImageView作为子视图,问题是UIImageView没有在UIScrollView中显示。
这是我的代码:
import UIKit
import Foundation
class InstagramBigCollectionViewCell: UICollectionViewCell {
private let scrollView = UIScrollView()
private let instagramImageThumb = UIImageView()
var urlString: String!
func initialize() {
scrollView.backgroundColor = UIColor.clearColor()
self.backgroundColor = UIColor.clearColor()
instagramImageThumb.backgroundColor = UIColor.redColor()
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 6.0
scrollView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action:#selector(self.zoomWhenDoubleTap))
tap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(tap)
self.addSubview(scrollView)
scrollView.topAnchor.constraintEqualToAnchor(self.topAnchor).active = true
scrollView.leadingAnchor.constraintEqualToAnchor(self.leadingAnchor).active = true
// scrollView.trailingAnchor.constraintEqualToAnchor(self.trailingAnchor).active = true
// scrollView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active = true
scrollView.contentSize = CGSize(width: self.frame.width, height: self.frame.height)
instagramImageThumb.contentMode = .ScaleAspectFit
scrollView.addSubview(instagramImageThumb)
instagramImageThumb.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
print(instagramImageThumb.frame)
//here we can see that the frame is set as well
}
func zoomWhenDoubleTap(){
let scrollViewSize = scrollView.bounds.size
var rectToZoomTo = CGRect()
let width = scrollViewSize.width
let height = scrollViewSize.height
let x:CGFloat = 0.0
let y:CGFloat = 0.0
rectToZoomTo = CGRect(x: x, y: y, width: width, height: height)
self.scrollView.zoomToRect(rectToZoomTo, animated: true)
}
}
extension InstagramBigCollectionViewCell: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return instagramImageThumb
}
}
知道为什么吗?
编辑:UIScrollView的边界已设置且框架在那里,但由于某种原因,UIImageView不会出现在屏幕上EDIT2:显示所有代码
初始化在cellForItemAt中调用:在UICollectionViewDataSouce中