試圖使用代碼創建的元素進行scrollView,但是我將這些元素重疊在一起。滾動查看本身在故事板中製作。這裏是我的代碼:滾動視圖中的重疊元素
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView! {
didSet {
scrollView.backgroundColor = .yellow
}
}
lazy var im: UIImageView = {
let im = UIImageView(frame: CGRect(x: 10, y: 74, width: self.view.frame.size.width - 20, height: 200))
im.backgroundColor = .white
return im
}()
lazy var label: UILabel = {
let l = UILabel(frame: CGRect(x: 10, y: 284, width: self.view.frame.size.width - 20, height: CGFloat.greatestFiniteMagnitude))
l.numberOfLines = 0
l.lineBreakMode = .byWordWrapping
l.text = "crazy amount of text"
l.sizeToFit()
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: l.frame.height + 280)
return l
}()
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.addSubview(im)
self.scrollView.addSubview(label)
}
}
白色矩形是ImageView的!
如何防止重疊這些元素?
哪部分重疊? – KKRocks
@KKRocks白色矩形是imageView。因此,imageView被應該追隨的文本重疊。 –