我有一个UIScrollView。目前,我只是将其设置为屏幕高度的两倍(在这种情况下,框架只是UIScreen.mainScreen().bounds
):
class VenueDetailView: UIScrollView {
required init?(coder aDecoder: NSCoder) { fatalError("Storyboard makes me sad.") }
override init(frame: CGRect) {
super.init(frame: frame)
contentSize = CGSize(width: frame.width, height: frame.height*2) <----------------
backgroundColor = UIColor.greenColor()
}
func addBannerImage(imageUrl: NSURL) {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 300))
// TODO: Make this asynchronous
// Nice to have: cache the image
if let data = NSData(contentsOfURL: imageUrl) {
imageView.image = UIImage(data: data)
}
addSubview(imageView)
}
}
然而,我希望它的大小与其内部所有内容的大小相同。我该怎么做?这是否意味着在添加所有子视图后我必须设置contentSize
?