如何在任意大小的设备上将scrollview的内容大小设置为最大帧宽

时间:2015-05-27 02:54:04

标签: ios iphone swift uiscrollview contentsize

我在我的故事板中创建了一个任何宽度和任何高度的UIScrollview,我试图将内容大小宽度设置为在其上查看的设备上的框架宽度。无论我尝试什么,宽度都被设置为故事板上显示的宽度,584,即使在iPhone 6模拟器上运行它,它应该是大约300.这导致scrollview具有滚动的能力横向,我不想要。我确实设法通过添加UIView作为子视图来使scrollview停止水平滚动。但是,我仍然无法弄清楚如何获得正确的宽度尺寸。我不仅尝试设置conent大小宽度,而且我还尝试在scrollview中设置UILabel,其宽度与uiscrollview的内容大小相同,但是获取scrollview的框架宽度或边界宽度以及UIView子视图太大了。任何帮助都将不胜感激。

scrollview位于自定义表格视图单元格内。这是单元格的类。

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)

    }

}

1 个答案:

答案 0 :(得分:1)

问题仅在于您过早地设置contentSize。在调用awakeFromNib时,滚动视图(以及一般的界面)尚未获得其大小,因此,正如您所说,您将contentSize宽度设置为滚动视图' s 宽度 - 它在故事板中的宽度 - 因为它仍然具有旧宽度。

awakeFromNib中取出代码并将其放入布局后运行的地方 - 例如viewDidLayoutSubviews