【问题标题】:UIScrollView not able to scroll?UIScrollView 无法滚动?
【发布时间】:2018-03-30 15:26:46
【问题描述】:

我将这个UIScrollView 设置为UIImageViewUILabel。 但是,它并没有像我想要的那样滚动。就好像它甚至不存在......我该如何解决这个问题?我只是希望它是可滚动的(即使底部没有任何东西可以滚动)

提前谢谢!

这是我的代码:

    // scrollView
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(scrollView)

    // scrollView constraints
    scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true

    // Initialize profileImageView
    profileImageView.image = #imageLiteral(resourceName: "neil")
    profileImageView.translatesAutoresizingMaskIntoConstraints = false
    profileImageView.layer.cornerRadius = 125 / 2
    profileImageView.clipsToBounds = true
    scrollView.addSubview(profileImageView)

    // Add profileImageView constraints
    profileImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 45).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 125).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 125).isActive = true
    profileImageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    // Add separator view
    seperator.translatesAutoresizingMaskIntoConstraints = false
    seperator.backgroundColor = UIColor.darkGray
    scrollView.addSubview(seperator)

    // seperator constraints
    seperator.heightAnchor.constraint(equalToConstant: 2).isActive = true
    seperator.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.8).isActive = true
    seperator.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    seperator.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 20).isActive = true

    //nameLabel
    let nameLabelFont = UIFont.monospacedDigitSystemFont(ofSize: 36, weight: UIFont.Weight.heavy)
    nameLabel.font = nameLabelFont
    nameLabel.text = currentUser.name
    nameLabel.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(nameLabel)

    // nameLabel constraints
    nameLabel.topAnchor.constraint(equalTo: seperator.bottomAnchor, constant: 10).isActive = true
    nameLabel.leadingAnchor.constraint(equalTo: seperator.leadingAnchor).isActive = true

【问题讨论】:

    标签: ios iphone swift uiscrollview


    【解决方案1】:

    您无法滚动,因为scrollView.contentSize 没有您的视野那么大。

    如果你想让你的屏幕可滚动,你应该设置scrollView.contentSize

    scrollView.contentSize = CGSize(width: 1000, height: 1000) 放在view.addSubview(scrollView) 之前将可滚动区域设置为1000x1000。

    通常scrollView.contentSize 应根据UIScrollView 内的 UI 元素计算,例如大小为 10 张图片,中间有空格。

    【讨论】:

    • 非常感谢,帮了大忙!
    猜你喜欢
    • 2021-03-23
    • 2011-03-26
    • 2018-02-03
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多