UIScrollView contentSize not working

前端 未结 15 1581
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 06:18

I put a UIScrollView in my nib\'s view, and linked it to a an IBOutlet property.

Now, when I do this in my viewDidLoad

15条回答
  •  渐次进展
    2020-11-28 07:06

    I finally worked out my own solution to this problem because in my case I couldn't use the view controller's life cycle. Create your own scroll view subclass and use it instead of UIScrollView. This even worked for a scroll view inside a collection view cell.

     class MyScrollView:UIScrollView {
    
        var myContentSize:CGSize = CGSize.zero  // you must set this yourself 
    
        override func layoutSubviews() {
           super.layoutSubviews()
           contentSize = myContentSize
        }
    
     }
    

    My MyScrollView was defined in the nib with a tag of 90. If so this is a good way to set content size in the code in the parent view.

    let scrollView = viewWithTag(90) as! MyScrollView      
    scrollView.myContentSize = ...
    

提交回复
热议问题