ios - UIScrollView 无法滚动?

标签 ios iphone swift uiscrollview

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

提前致谢!

这是我的代码:

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

最佳答案

您无法滚动,因为 scrollView.contentSize 没有您的 View 大。

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

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

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

关于ios - UIScrollView 无法滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46815529/

相关文章:

ios - 更新购物车中的产品数量(而非数量) - Shopify Mobile Buy SDK

ios - 如何找出错误运行命令 - 失败! [错误] 异常 : Using cordova-fetch for cordova-ios@~4. 4.0

ios - 在 iOS8.0.2 上未收到 Pushkit voip 推送通知

ios - 在索引处添加 NSMutableArray 的元素与其他 NSMutableArrays 的相应元素

ios - 从 View Controller 加载 Tabbar Controller

iphone - 使用 NSBundle 找不到包中的图像

swift - 获取 SequenceType 的前 n 个元素的优雅方法

Swift 命名空间冲突

ios - 如何在 Swift 中使用 AgoraRtcEngineKit 创建 channel 和服务器 token ?

ios - iframe 在 ios 上滚动固定 div 不起作用