Search code examples
iosswiftuiscrollview

Programmatic UIScrollView doesn't want to scroll


he developer, i know this question been ask a lot of time in stackoverflow. but still i have trouble create uiscrollview programmatically, even in storyboard is so confusing to setup uiscrollview because it effect another view which is find when i setup autolayout. and my question is my scrollview doesn't want to scroll inside a container view. i think i setup it correctly. this is my code.

    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)
    }    

Solution

  • i found the answer, i browse in youtube how to setup UIScrollView. I think UIScrollView is a special case in UIKit, since it's not behavior correctly with autolayout trying to hugging scrollview leading and trailing. so this is the answer for try to setup UIScrollView Programmatically.

    // 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)
            ])