Search code examples
iosiphoneswiftuiscrollviewcontentsize

How to set content size of scrollview to maximum width of frame on any size device


I created a UIScrollview in my storyboard with any width and any height, and I am trying to set the content size width to the width of the frame on the device that it is viewed on. No matter what I try, the width is being set to the width that was displayed on the storyboard, 584, even though when running it on the iPhone 6 simulator, it should be around 300. This results in the scrollview having the ability to scroll horizontally, which I do not want. I did manage to get the scrollview to stop scrolling horizontally by adding a UIView as a subview. However, I still can't figure out how to get the correct size for the width. Not only am I trying to set the conent size width, but I am also trying to set a UILabel inside the scrollview with the same width as the content size of the uiscrollview, but getting the frame width or the bounds width of the scrollview and the UIView subview are too large. Any help with this would be appreciated.

The scrollview is inside a custom table view cell. Here is the class for the cell.

import UIKit

class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {


    @IBOutlet weak var winLoseValueLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var newTotalLabel: UILabel!
    @IBOutlet weak var locationLabel: UILabel!
    @IBOutlet weak var playersScrollView: CustomTableCellScrollView!
    @IBOutlet weak var scrollContentView: UIView!

    var numLinesInScrollView : CGFloat?
    var tblView : UITableView?
    var people : String?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.playersScrollView.tblView = tblView
        numLinesInScrollView = 0.0
        //self.playersScrollView.contentSize.width = self.scrollContentView.frame.width
        self.playersScrollView.contentSize.width = self.playersScrollView.frame.size.width
        self.playersScrollView.contentSize.height = 100
        //self.playersScrollView.contentSize.width = UIScreen.mainScreen().bounds.width - 24
        self.playersScrollView.addSubview(self.scrollContentView)
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func addLabelToScrollView(str : String) {

        // Increment the number of lines in the scrollview
        if numLinesInScrollView != nil {
            numLinesInScrollView!++
        }
        else{
            numLinesInScrollView = 1
        }

        // Get the bounds of the screen
        let screenSize : CGRect = UIScreen.mainScreen().bounds

        // Update the height of the scrollview
        self.playersScrollView.contentSize.height = 20 * numLinesInScrollView!

        // Add a new label to the players scroll view
        let w : CGFloat = self.scrollContentView.frame.width - 350
        let h : CGFloat = 20
        let x : CGFloat = 0
        let y : CGFloat = (numLinesInScrollView! - 1) * h
        let frame : CGRect = CGRect(x: x, y: y, width: w, height: h)
        var person : UILabel = UILabel(frame: frame)
        person.font = UIFont(name: "HelveticaNeue-Bold", size: 12.0)
        person.textColor = UIColor.blackColor()
        person.textAlignment = NSTextAlignment.Center
        switch numLinesInScrollView!{
            case 1:
                person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0)
            case 2:
                person.backgroundColor = UIColor(red: 0, green: 1.0, blue: 0, alpha: 1.0)
            case 3:
                person.backgroundColor = UIColor(red: 0, green: 0, blue: 1.0, alpha: 1.0)
            case 4:
                person.backgroundColor = UIColor(red: 1.0, green: 0, blue: 1.0, alpha: 1.0)
            default:
                break
        }
        person.text? = "Hellow World"
        self.scrollContentView.addSubview(person)

    }

}

Solution

  • The problem is merely that you are setting the contentSize too soon. At the time awakeFromNib is called, the scroll view (and interface in general) has not yet acquired its size, so you are, as you say, setting the contentSize width to the scroll view's old width — the width it had in the storyboard — because it still has its old width.

    Take the code out of awakeFromNib and put it into a place that runs after layout has occurred — like, for example, viewDidLayoutSubviews.