Programmati c UIScrollView не хочет прокручивать - PullRequest
1 голос
/ 22 марта 2020

он разработчик, я знаю, что этот вопрос много раз задавался в stackoverflow. но все же у меня возникают проблемы с программным созданием uiscrollview, даже в раскадровке так сложно запутать настройку uiscrollview, потому что это влияет на другое представление, которое можно найти при настройке autolayout. и мой вопрос заключается в том, что мое представление прокрутки не хочет прокручивать внутри представления контейнера. я думаю, что настроил это правильно. это мой код.

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

1 Ответ

0 голосов
/ 22 марта 2020

Я нашел ответ, я смотрю в YouTube, как настроить UIScrollView. Я думаю, что UIScrollView является особым случаем в UIKit, так как он не ведет себя правильно, когда autolayout пытается обнять начальные и конечные точки прокрутки. так что это ответ на попытку настроить 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)
        ])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...