UIScrollView 表示滚动但不在视图中滚动

Posted

技术标签:

【中文标题】UIScrollView 表示滚动但不在视图中滚动【英文标题】:UIScrollView indicates scrolling but doesnt scroll in view 【发布时间】:2016-01-15 10:50:52 【问题描述】:

我有一个滚动视图,其内容视图大于使用约束的实际滚动视图。在这个 contentview uiview 我设置了我的所有内容,它表明它是可滚动的。向下滚动时滚动指示器会移动,但实际上并不滚动。

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

可以看到scrollContentView的设置为最新元素的底部。

我不知道为什么会这样。

【问题讨论】:

【参考方案1】:

将此代码添加到您的视图控制器:

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

【讨论】:

以上是关于UIScrollView 表示滚动但不在视图中滚动的主要内容,如果未能解决你的问题,请参考以下文章

滚动视图不在自动布局 xcode 6.4 中滚动

UITableView 不在 UIScrollView 内滚动

滚动条滚动但 UIScrollView 不滚动

滚动视图位置中的 UIButton 意外

滚动时如何在 UIScrollView 中固定图像/视图

uiscrollview 不能滚动到底部