我有一个 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
添加所有子视图后?
这是否意味着我必须在添加所有子视图后设置 contentSize ?
基本上是的。您的目标应该是使滚动视图变小而内容大小变大。这就是滚动视图可滚动的原因:它的contentSize
大于其自身的边界大小。因此,将滚动视图本身设置为frame
这与bounds
其超级视图,但随后设置其contentSize
接受它的所有子视图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)