ios - 无法使用 PureLayout 设置 ScrollView

标签 ios swift uiscrollview autolayout pure-layout

我在 UIViewController 子类中有以下 viewDidLoad 方法实现:

var scrollView  = UIScrollView.newAutoLayoutView()
var contentView = UIView.newAutoLayoutView()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(scrollView)
    scrollView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)

    scrollView.addSubview(contentView)
    contentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
    contentView.autoMatchDimension(.Height, toDimension: .Height, ofView: view)
    contentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)

    contentView.addSubview(customView)
    customView.autoPinEdgeToSuperviewEdge(.Top, withInset:0)
    customView.autoPinEdgeToSuperviewEdge(.Left, withInset:15)
}

但是当我运行应用程序时,内容不会滚动。我发现很少PureLayout文档和示例,对 ScrollView 一无所知。有人可以帮我解决这个问题吗?

最佳答案

当您将自动布局与 Scrollview 一起使用时,您必须确保 contentView 中的每个 View 都固定到所有 4 个边。

您的 customView 仅固定到顶部和左侧,尝试固定到所有边并使用 autoSetDimension(.Height, toSize: 1300) 到一个大尺寸并观察它的工作:)

这是一个可以正常工作的示例代码

    // background
    view.backgroundColor = UIColor.lightGrayColor()

    // ScrollView setup
    let scrollView = UIScrollView()
    scrollView.backgroundColor = UIColor.blueColor()
    view.addSubview(scrollView)
    scrollView.autoPinEdgesToSuperviewEdges()

    let scrollContentView = UIView()
    scrollContentView.backgroundColor = UIColor.redColor()
    scrollView.addSubview(scrollContentView)
    scrollContentView.autoPinEdgesToSuperviewEdges()
    scrollContentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)

    let dummy = UIView()
    dummy.backgroundColor = UIColor.whiteColor()
    scrollContentView.addSubview(dummy)
    dummy.autoSetDimension(.Height, toSize: 1300)
    dummy.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsMake(20, 20, 20, 20))

关于ios - 无法使用 PureLayout 设置 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36117769/

相关文章:

ios - Xcode:构建失败:错误消息

ios - 在 iOS、Swift 中从 firestore 获取时,带有标签、图像、gif 和视频的 TableView 挂起/卡住不正确

swift - 为什么 Swift 结构中的私有(private)变量在该结构的#if'd init 中不可用?

iphone - 触摸结束 :withEvent: not called when a drag event moved out of the screen top/bottom

ios - ScrollView 当键盘出现时不滚动 如果 ContentView 中的元素

ios - UIScrollview 内的 UIView 内的 UILabel 上的 UIGestureRecognizer

ios - 虹吸调用不起作用 - pjsip

ios - 分段控制,更改 UIImageView (以编程方式)Swift 4/5

iphone - IOS App验证错误: An SSL error has occurred and secure connection to the server cannot be made

ios - ViewController 禁用交互 - iOS