相关文章推荐
任性的包子  ·  SqlAlchemy 2.0 ...·  1 年前    · 

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)

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.

An even better solution might be to learn how auto layout works in conjunction with scroll views to set the content size. Then you could set this all up in the nib/storyboard, and no code to set the content size would be needed. However, there's nothing wrong with what you are doing; your timing is off, that's all. – matt May 27 '15 at 3:07 It does not appear that UITableViewCell class has a method called viewDidLayoutSubviews. Do you know what method for this class would be called after the width has been updated? I found that the width isn't even correct in the addLabelToScrollView method. As for the auto layout suggestion, would this work if I want to set programmatically created UILabel to the width of the scroll view as well? Thanks! – Jason247 May 27 '15 at 3:11 I was assuming that the code would get moved into the view controller, just because that is where overall layout is usually governed. – matt May 27 '15 at 3:41 I guess I'm confused on how to move it to the view controller because the ScrollView is inside a custom table view cell, and I am not sure how to get a reference to each custom cell inside the viewDidLayoutSubviews method of the view controller that is a subclass of UITableViewController. – Jason247 May 27 '15 at 3:58 Okay, that makes sense. So how about layoutSubviews instead? But I have answered the question: the measurements are wrong just because you are making them too early. – matt May 27 '15 at 12:30

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.6.3.33858