Search code examples
iosxcodeswift3interface-builderscrollview

Xcode scrollview not working


I'm making a game and need users to sign an End User License Agreement (EULA). The EULA is long and requires a scroll view. I want the screen itself to stay still, so the buttons and header are always present:

Here is a screenshot of my viewController storyboard:

enter image description here

here's my code:

@IBOutlet var eulaView: UIView!
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var label: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    let width = eulaView.frame.width
    let height = eulaView.frame.height

    self.scrollView.frame = CGRect(x: 0, y: 0, width: width, height: height)

    self.label.frame = CGRect(x: 0, y: 0, width: width , height: 2000)
    self.label.text = "Lots of stuff"

}

Strangely, if I set self.label.frame's width to 100, it shows up. If I have it at 300, or "width", the label disappears.

No matter what, the scroll view doesn't scroll.

PS: I had it working earlier, but then I tried it on an iPhone X simulator and it was blank.


Solution

  • You need to set self.scrollView.contentSize = CGSize(width: width,height: 2000)

    Sometimes When you change device in storyboard at that time screen was blank may be its bug of xcode.

    To find the Dynamic Height Of label Depends on String Of label.

    func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
        let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.font = font
        label.text = text
        label.sizeToFit()
    
        return label.frame.height
    }