【问题标题】:Expand a ScrollView's height programmatically以编程方式展开 ScrollView 的高度
【发布时间】:2018-08-09 23:55:58
【问题描述】:

我现在正在尝试几个小时来解决这个问题,但这并不意味着这是一个困难的问题,因为我是一个完全的初学者。

我正在使用 swift 使用 XCode 9。

我创建了一个 ViewController,其中包含一个包含滚动视图的视图。 我的目标是能够以编程方式设置滚动视图的模拟高度(我可以滚动),这样我就可以使 ScrollView 展开。

我试过了:

ScrollView.frame = CGRect(x: 0, y: 0, width: ScrollView.frame.width, height: 3000)

将视图的高度设置为 3000,但它不起作用。如果我使用情节提要设置视图的高度,它仍然可以工作......

【问题讨论】:

    标签: ios swift xcode scrollview


    【解决方案1】:

    Scrollview 期望它的内容来自内部元素,反之亦然,或者您需要像这样设置它的 contentSize

    let scroll = UIScrollView(frame: self.view.frame)
    
    view.addSubview(scroll)
    
    let v1 = UIView(frame: self.view.frame)
    
    v1.backgroundColor = .red
    
    let v2 = UIView(frame: self.view.frame.offsetBy(dx: 0, dy: self.view.frame.height)) // set y of view 2 to be bottom of view 1
    
    v2.backgroundColor = .blue
    
    scroll.addSubview(v1)
    
    scroll.addSubview(v2)
    
    scroll.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height * 2 )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 2011-04-01
      • 2011-08-11
      相关资源
      最近更新 更多