gpt4 book ai didi

ios - subview 不滚动,但滚动条快速滚动

转载 作者:行者123 更新时间:2023-11-30 11:16:18 25 4
gpt4 key购买 nike

我正在以编程方式创建scrollView,但由于某些原因,我可以看到scrollView的滚动条,但 subview 没有滚动。在下面添加了我的代码

//create a closure for scroll view
let vcScrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.dark_background

return scrollView
}()

//create closure for dark view
let darkView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

//create a closure for offer label
let offerLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.white
return label
}()

override func viewDidLoad() {
super.viewDidLoad()
modalPresentationCapturesStatusBarAppearance = true

view.addSubview(vcScrollView)
setUpViews()


}

func setUpViews(){
//scrollView
vcScrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
vcScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
vcScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
vcScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
vcScrollView.contentSize.height = 1500

//Dark view
vcScrollView.addSubview(darkView)
darkView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
darkView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
darkView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
darkView.heightAnchor.constraint(equalToConstant: 130).isActive = true

//Offer Label
darkView.addSubview(offerLabel)
offerLabel.text = selectedOffer
offerLabel.font = UIFont(name: String.defaultFontR, size: 29)
offerLabel.textAlignment = .center
offerLabel.topAnchor.constraint(equalTo: darkView.topAnchor, constant: 95).isActive = true
offerLabel.leadingAnchor.constraint(equalTo: darkView.leadingAnchor, constant: 20).isActive = true
offerLabel.trailingAnchor.constraint(equalTo: darkView.trailingAnchor, constant: -20).isActive = true
}

也将 ScrollView 添加为 View 的 subview 。提前致谢

最佳答案

问题是您的 ScrollView 的子级正在使用 View 的 anchor 。将它们切换为使用 scrollView 的 anchor 。此外,您可以使用宽度约束来代替尾随约束。这将允许它向上和向下滚动。

//Dark view
vcScrollView.addSubview(darkView)
darkView.leadingAnchor.constraint(equalTo: vcScrollView.leadingAnchor).isActive = true
darkView.topAnchor.constraint(equalTo: vcScrollView.topAnchor).isActive = true
darkView.widthAnchor.constraint(equalTo: vcScrollView.widthAnchor).isActive = true
darkView.heightAnchor.constraint(equalToConstant: 130).isActive = true

如果您想在 darkView 下包含 View ,则需要提供 subview 的底部 anchor 。

关于ios - subview 不滚动,但滚动条快速滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com