简体   繁体   中英

UIScrollView indicates scrolling but doesnt scroll in view

I have a scrollview with a contentview which is bigger than the actual scrollview using constraints. In this contentview uiview I set all my content and it indicates that its scrollable. The scrollindicator moves when scrolling down, but it doesn't actually scroll.

// MARK: - Methods
// MARK: - Private
private func setupViews()
{
    // Scroll View
    scrollView = UIScrollView()
    addSubview(scrollView)

    // Scroll Content View
    scrollContentView = UIView()
    scrollView.addSubview(scrollContentView)

    // Image View
    settingsImageView = UIImageView()
    settingsImageView.image = UIImage(named: "CheckSettings")
    scrollContentView.addSubview(settingsImageView)

    // Title label
    settingsTitleLabel = UILabel()
    settingsTitleLabel.text = NSLocalizedString("checkSettingsTitle", comment: "")
    settingsTitleLabel.textColor = .whiteColor()
    settingsTitleLabel.font = UIFont(name: ApplicationFont.bold, size: Design.Constraints.settingsTitleLabelFontSize)
    settingsTitleLabel.numberOfLines = 0
    settingsTitleLabel.textAlignment = .Center
    settingsTitleLabel.lineBreakMode = .ByWordWrapping
    scrollContentView.addSubview(settingsTitleLabel)

    // Lab test reminder
    labTestReminderView = LMSettingView(frame: CGRectZero, title: NSLocalizedString("titleLabTestReminder", comment: ""), stringValue: "Yes, Monthly 10:00 AM starting at 16 May 2015", boolValue: true)
    scrollContentView.addSubview(labTestReminderView)

    // Self check reminder
    selfCheckReminderView = LMSettingView(frame: CGRectZero, title: NSLocalizedString("titleSelfCheckReminder", comment: ""), stringValue: "Yes, Monthly 12:45 PM starting today", boolValue: true)
    scrollContentView.addSubview(selfCheckReminderView)

    // Weekly tip
    weeklyTipView = LMSettingView(frame: CGRectZero, title: NSLocalizedString("titleWeeklyTips", comment: ""), stringValue: "On", boolValue: true)
    scrollContentView.addSubview(weeklyTipView)
}
private func setupConstraints()
{
    // Scroll View
    scrollView.snp_makeConstraints { make in
        make.top.equalTo(snp_top)
        make.left.equalTo(snp_left)
        make.right.equalTo(snp_right)
        make.bottom.equalTo(snp_bottom)
    }

    // Scroll Content View
    scrollContentView.snp_makeConstraints { make in
        make.edges.equalTo(scrollView.snp_edges)
        make.width.equalTo(scrollView.snp_width)
        make.bottom.equalTo(weeklyTipView.snp_bottom)
    }

    // Image View
    settingsImageView.snp_makeConstraints { make in
        make.top.equalTo(snp_top).offset(Design.Constraints.settingsImageViewTopOffset)
        make.centerX.equalTo(snp_centerX)
    }

    // Title label
    settingsTitleLabel.snp_makeConstraints { make in
        make.top.equalTo(settingsImageView.snp_bottom).offset(Design.Constraints.settingsTitleLabelTopOffset)
        make.left.equalTo(snp_left).offset(Design.Constraints.settingsLeftRightOffset)
        make.right.equalTo(snp_right).inset(Design.Constraints.settingsLeftRightOffset)
    }

    // Lab test reminder
    labTestReminderView.snp_makeConstraints{ make in
        make.top.equalTo(settingsTitleLabel.snp_bottom).offset(Design.Constraints.labTestReminderViewTopOffset)
        make.left.equalTo(snp_left)
        make.right.equalTo(snp_right)
        make.height.equalTo(Design.Constraints.settingsViewHeight)
    }

    // Self check reminder
    selfCheckReminderView.snp_makeConstraints{ make in
        make.top.equalTo(labTestReminderView.snp_bottom)
        make.left.equalTo(snp_left)
        make.right.equalTo(snp_right)
        make.height.equalTo(Design.Constraints.settingsViewHeight)
    }

    // Weekly tip
    weeklyTipView.snp_makeConstraints{ make in
        make.top.equalTo(selfCheckReminderView.snp_bottom)
        make.left.equalTo(snp_left)
        make.right.equalTo(snp_right)
        make.height.equalTo(Design.Constraints.settingsViewHeight)
    }
}

You can see that the of the scrollContentView is set to the bottom of the latest element.

I have no clue why this happens.

Add this code to your view controller:

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    scrollView.contentSize = scrollContentView.bounds.size
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM