【问题标题】:Unable to set up a scroll view with PureLayout无法使用 PureLayout 设置滚动视图
【发布时间】:2016-03-20 18:27:34
【问题描述】:

我在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 文档和示例,并且对滚动视图一无所知。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: ios swift uiscrollview autolayout pure-layout


    【解决方案1】:

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

    您的 customView 仅固定到 TOP 和 LEFT,尝试固定到所有边并使用 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))
    

    【讨论】:

      【解决方案2】:

      这可能对某人有帮助,

      func updateUI(){
      
      
      
          self.view.backgroundColor = .white
          self.view.addSubview(scrollView)
          scrollView.addSubview(contentView)
               contentView.addSubview(offer_title)
               contentView.addSubview(offer_image)
               contentView.addSubview(body)
      
          scrollView.autoPinEdgesToSuperviewEdges()
          contentView.autoPinEdgesToSuperviewEdges()
          contentView.autoMatch(.width, to: .width, of: view)
      
      
      
               offer_image.contentMode = .scaleToFill
               offer_image.autoPinEdge(toSuperviewEdge: .top, withInset: 0)
               offer_image.autoPinEdge(toSuperviewEdge: .trailing, withInset: 0)
               offer_image.autoPinEdge(toSuperviewEdge: .leading, withInset: 0)
               offer_image.autoSetDimension(.height, toSize: 300)
      
               offer_title.autoPinEdge(.top, to: .bottom, of: offer_image, withOffset: 8)
               offer_title.autoPinEdge(toSuperviewEdge: .trailing, withInset: 8)
               offer_title.autoPinEdge(toSuperviewEdge: .leading, withInset: 8)
               offer_title.autoSetDimension(.height, toSize: 60)
               body.numberOfLines = 0
               body.font = UIFont(name: "HelveticaNeueLT-Regular", size: 20)
               body.autoPinEdge(.top, to: .bottom, of: offer_title, withOffset: 8)
               body.autoPinEdge(toSuperviewEdge: .trailing, withInset: 8)
               body.autoPinEdge(toSuperviewEdge: .leading, withInset: 8)
               body.autoPinEdge(toSuperviewEdge: .bottom, withInset: 8)
           }
      

      然后在 viewdidLoad

      中调用它

      【讨论】:

        猜你喜欢
        • 2014-08-06
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-07
        • 1970-01-01
        相关资源
        最近更新 更多