UIScrollView contentSize not working

前端 未结 15 1588
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 06:18

I put a UIScrollView in my nib\'s view, and linked it to a an IBOutlet property.

Now, when I do this in my viewDidLoad

15条回答
  •  萌比男神i
    2020-11-28 07:18

    I got Autolayout to work for paginated scroll views whose pages occupy the full-width of the screen. The pages automatically resize according to the scroll view's size. I haven't tested this for lesser-width scroll views but do comment away if it works--I beleieve it should. Targeted for iOS 9, wrote code in Swift 2, used a mix of IB's and custom code in awakeFromNib.

    Steps:

    • Define a full-screen scroll view.
    • Inside the scroll view, add a UIView (I called mine contentView) whose top, trailing, bottom, and leading edges to the scroll view are all zero; the height is equal to the scroll view's; but the width is the scroll view's width times the number of pages. If you're doing this visually, you will see your content view extend beyond your scroll view in Inteface Builder.
    • For every "page" inside the contentView, add Autolayout rules to put them side-by-side each other, but most importantly, give them each a constraint so that their widths are equal to the scroll view's, not the content view's.

    Sample code below. embedChildViewController is just my convenience method for adding child VCs--do look at setupLayoutRulesForPages. I have exactly two pages so the function is too simple, but you can expand it to your needs.

    In my view controller:

    override func loadView() {
        self.view = self.customView
    }
    
    override func viewDidLoad() {
    super.viewDidLoad()
    
    self.embedChildViewController(self.addExpenseVC, toView: self.customView.contentView, fillSuperview: false)
    self.embedChildViewController(self.addCategoryVC, toView: self.customView.contentView, fillSuperview: false)
    self.customView.setupLayoutRulesForPages(self.addExpenseVC.view, secondPage: self.addCategoryVC.view)
    }
    

    My custom view:

    class __AMVCView: UIView {
    
        @IBOutlet weak var scrollView: UIScrollView!
        @IBOutlet weak var contentView: UIView!
        @IBOutlet weak var pageControl: UIPageControl!
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            self.scrollView.pagingEnabled = true
            self.scrollView.bounces = true
            self.scrollView.showsHorizontalScrollIndicator = false
            self.scrollView.showsVerticalScrollIndicator = false
    
            self.pageControl.numberOfPages = 2
    
            self.contentView.backgroundColor = UIColor.blueColor()
            self.scrollView.backgroundColor = UIColor.clearColor()
            self.backgroundColor = UIColor.blackColor()
        }
    
        func setupLayoutRulesForPages(firstPage: UIView, secondPage: UIView) {
            guard self.contentView.subviews.contains(firstPage) && self.contentView.subviews.contains(secondPage)
                else {
                    return
            }
    
            let rules = [
                "H:|-0-[firstPage]-0-[secondPage]-0-|",
                "V:|-0-[firstPage]-0-|",
                "V:|-0-[secondPage]-0-|"
            ]
            let views = [
                "firstPage" : firstPage,
                "secondPage" : secondPage
            ]
            let constraints = NSLayoutConstraint.constraintsWithVisualFormatArray(rules, metrics: nil, views: views)
    
            UIView.disableAutoresizingMasksInViews(firstPage, secondPage)
            self.addConstraints(constraints)
    
            // Add the width Autolayout rules to the pages.
            let widthConstraint = NSLayoutConstraint(item: firstPage, attribute: .Width, relatedBy: .Equal, toItem: self.scrollView, attribute: .Width, multiplier: 1, constant: 0)
            self.addConstraint(widthConstraint)
        }
    
    }
    

提交回复
热议问题