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

时间:2016-01-15 10:50:53

标签: ios swift uiscrollview

我有一个内容视图的滚动视图,它比使用约束的实际滚动视图大。在这个内容视图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 个答案:

答案 0 :(得分:0)

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

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