开发人员,我知道这个问题在stackoverflow中问了很多时间。但我仍然无法以编程方式创建uiscrollview,即使在情节提要中也难以设置uiscrollview,因为它会影响我在设置自动布局时找到的另一个视图。我的问题是我的scrollview不想在容器视图内滚动。我想我正确设置了。这是我的代码。
let containerTitle = GView(bgColor: .white, radius: 0)
let headerView = HeaderView()
let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.backgroundColor = .white
scrollView.translatesAutoresizingMaskIntoConstraints = false
return scrollView
}()
let chooseScheduleDropDown = GDropdownSchedule(type: .system)
let entryView = GEntryTextField()
let chooseDateView = GChooseDateView()
let chooseClass = GChooseClassView()
let startTimeView = GStartTimeView()
let endTimeView = GEndTimeView()
let descriptionView = GDescriptionView()
let saveBtn = GActionBtn(title: "Simpan", bgColor: #colorLiteral(red: 0.4470588235, green: 0.6274509804, blue: 0.3960784314, alpha: 1), cornerRadius: 27.5)
override func viewDidLoad() {
super.viewDidLoad()
scrollView.isScrollEnabled = true
scrollView.contentSize = CGSize(width: containerTitle.bounds.width, height: containerTitle.bounds.height * 2)
view.addSubview(containerTitle)
containerTitle.layer.cornerRadius = 10
containerTitle.clipsToBounds = true
containerTitle.anchor(top: view.safeAreaLayoutGuide.topAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, topPadding: 16, rightPadding: 19, bottomPadding: 16, leftPadding: 19, width: 0, height: 0)
containerTitle.addSubview(headerView)
headerView.anchor(top: containerTitle.topAnchor, trailing: containerTitle.trailingAnchor, bottom: nil, leading: containerTitle.leadingAnchor, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 53)
containerTitle.addSubview(scrollView)
scrollView.anchor(top: headerView.bottomAnchor, trailing: containerTitle.trailingAnchor, bottom: containerTitle.bottomAnchor, leading: containerTitle.leadingAnchor, topPadding: 8, rightPadding: 8, bottomPadding: 8, leftPadding: 8, width: 0, height: 0)
scrollView.addSubview(chooseScheduleDropDown)
chooseScheduleDropDown.anchor(top: scrollView.topAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: 25, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 285, height: 60)
scrollView.addSubview(entryView)
entryView.anchor(top: chooseScheduleDropDown.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: heightView)
scrollView.addSubview(chooseDateView)
chooseDateView.anchor(top: entryView.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: heightView)
scrollView.addSubview(chooseClass)
chooseClass.anchor(top: chooseDateView.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: heightView)
scrollView.addSubview(startTimeView)
startTimeView.anchor(top: chooseClass.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: heightView)
scrollView.addSubview(endTimeView)
endTimeView.anchor(top: startTimeView.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: heightView)
scrollView.addSubview(descriptionView)
descriptionView.anchor(top: endTimeView.bottomAnchor, trailing: scrollView.trailingAnchor, bottom: nil, leading: scrollView.leadingAnchor, topPadding: topPadding, rightPadding: padding, bottomPadding: 0, leftPadding: padding, width: 0, height: 108)
scrollView.addSubview(saveBtn)
saveBtn.anchor(top: nil, trailing: scrollView.trailingAnchor, bottom: scrollView.bottomAnchor, leading: scrollView.leadingAnchor, topPadding: 0, rightPadding: 59, bottomPadding: 10, leftPadding: 59, width: 0, height: 41)
}
我找到了答案,我在youtube中浏览了如何设置UIScrollView。我认为UIScrollView是UIKit中的特例,因为在自动布局中尝试拥抱scrollview的前导和尾随时,它的行为不正确。因此,这就是尝试以编程方式设置UIScrollView的答案。
// i change the setup for scrollView contentSize
scrollView.contentSize.height = 630
// than use this constraint instead using leading and trailing, it fix using centerXAnchor.
containerTitle.addSubview(scrollView)
scrollView.anchor(top: headerView.bottomAnchor, trailing: containerTitle.trailingAnchor, bottom: containerTitle.bottomAnchor, leading: containerTitle.leadingAnchor, topPadding: 8, rightPadding: 8, bottomPadding: 8, leftPadding: 8, width: 0, height: 0)
scrollView.addSubview(chooseScheduleDropDown)
chooseScheduleDropDown.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
chooseScheduleDropDown.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
chooseScheduleDropDown.topAnchor.constraint(equalTo: scrollView.topAnchor),
chooseScheduleDropDown.widthAnchor.constraint(equalToConstant: 285),
chooseScheduleDropDown.heightAnchor.constraint(equalToConstant: 60)
])