0

我想scrollView在视图加载到屏幕时进行配置。但是在使用时内容大小没有按预期工作AutoLayout。如果放置在ViewDidAppear它按预期工作但我希望我的滚动视图配置ViewDidLoad方法

@IBOutlet weak var addressScrollView: UIScrollView!{
    didSet{

        self.addressScrollView.pagingEnabled = true
        self.addressScrollView.pagingEnabled = true
        self.addressScrollView.showsHorizontalScrollIndicator = false

    }
}

var billQuotes = ["This is bill","Bill Smokes","Bill drinks","But Still Codes"]
var viewFrame: CGRect = CGRectMake(0, 0, 0, 0)

override func viewDidLoad() {
    super.viewDidLoad()
   configureScrollView()
}


override func viewDidLayoutSubviews() {
    self.addressScrollView.contentSize = CGSizeMake(self.addressScrollView.frame.size.width * CGFloat(billQuotes.count+1), self.addressScrollView.frame.size.height)
}
override func viewDidAppear(animated: Bool) {
    //if placed in view did appear it works as expected but i want it to
}
func configureScrollView(){

    for bill in billQuotes{

        var index = find(billQuotes,bill)
        viewFrame.size = self.addressScrollView.frame.size
        viewFrame.size.width = self.addressScrollView.frame.size.width - 20
        viewFrame.origin.x = ((self.addressScrollView.frame.size.width) * CGFloat(index!)) + 10
        let addressView = UIView()
        addressView.layer.borderWidth = 2.0
        addressView.layer.borderColor = UIColor.grayColor().CGColor
        addressView.layer.cornerRadius = 5
        addressView.frame = viewFrame
        addressView.backgroundColor = UIColor(red: 0.847, green: 0.847, blue: 0.843, alpha: 1.00)

        var billLabel = UILabel(frame: CGRectMake(32, 8, addressView.frame.size.width - 34, 30))
        billLabel.textAlignment = NSTextAlignment.Left
        billLabel.text = "\(bill)"
        billLabel.font = UIFont.systemFontOfSize(20)
        billLabel.textColor = UIColor(red: 0.337, green: 0.341, blue: 0.345, alpha: 1.00)
        addressView.addSubview(billLabel)
        addressScrollView.addSubview(addressView)
    }

}

已更新:我希望我的滚动视图在 viewDidLoad 中配置,以便在视图出现后的几分之一秒后立即看到

4

3 回答 3

0

问题是当你调用它configureScrollView()时它还没有运行,所以它还不知道它是自己的。因此,当您在其中配置它时,它将被正确配置为随后将更改的框架(可能在此处和此处)。In已经被执行,所以这就是它在这里工作的原因,因为现在有它正确的.viewDidLoadlayoutSubviewsUIScrollViewframesuperviewviewDidLoadviewself.view.frame600widthheightviewDidAppearviewDidLayoutSubviewssuperviewframe

您可以做的是layoutSubviews在配置UIScrollView. 我不太了解需要强制执行哪些视图背后的逻辑,因此您必须尝试一下。但大体上是这样的:

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.setNeedsLayout() // Not sure if this is needed
    self.view.layoutIfNeeded() // Not sure if this is needed

    self.addressScrollView.setNeedsLayout()
    self.addressScrollView.layoutIfNeeded()
    configureScrollView()

    self.addressScrollView.contentSize = CGSizeMake(self.addressScrollView.frame.size.width * CGFloat(billQuotes.count+1), self.addressScrollView.frame.size.height)
}
于 2016-03-02T07:30:20.653 回答
0

尝试根据您在滚动视图中的视图(, )设置UIScrollViewin的 contentsizeviewDidLoadaddressViewbillLabel

ScrollView.contentSize = CGSizeMake(<Calculate width>, <Calculate height>)
于 2016-03-02T06:30:04.587 回答
0

您应该覆盖 viewWillLayoutSubviews 并将 configureScrollView() 放在那里。

于 2016-03-02T06:32:01.400 回答