0

On my iPhone 7 plus it's working fine, but on the simulator iPhone 5 SE the UIView doesn't fill the screen only after I switch tabs or I scroll the view.

enter image description here

I'm using XLPagerTabStrip (9.0.0) to obtain two pages.

I added a UIView with the blue background for debugging purposes, but what I want tot achieve is a list with books.

I want the UICollectionView items to fill the entire screen, this is my implementation:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

    print(String(format: "width is: %f", collectionView.bounds.width))

    return CGSize(width: collectionView.bounds.width, height: CGFloat(90))
}

This is what I got in the console: width is: 226.000000, but after I scroll or I switch tabs, I get the following result: width is: 320.000000

I really don't know what's going on, the AutoLayout constraints are being set via storyboard from top-to-bottom and leading-to-trailing to fill the entire screen:

enter image description here

Please note: In the storyboard I've set the blue color to UIView because I wanted to know where the width error comes from. (I guess it's coming from elsewhere and not from the UICollectionView, because as you can see the UIView doesn't fill the entire screen horizontally).

I've added some debug logs to see the lifecycle of the View Controllers.

[ParentViewController]: viewControllers()
[ChildViewController]: viewDidLoad()
[ParentViewController]: viewDidLoad()
[ChildViewController]: viewWillAppear()
[ParentViewController]: viewWillAppear()
[ParentViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidLayoutSubviews()
[ChildViewController]: viewDidAppear()
[ParentViewController]: viewDidAppear()

(where: ParentViewController is the main View Controller, which holds the two child page View Controllers)

I don't know why the [ParentViewController]: viewControllers() is called before viewDidLoad(). Maybe the error comes from here?

This is my implementation:

class ParentViewController: ButtonBarPagerTabStripViewController {

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

    private func configureButtonBar() {
        // set the button bar style (color, font size, green bar size, paddings, etc)
    }

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {        
        let child1 = UIStoryboard.init(name: "ParentStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
        child1.pageTitle = "TAB 1"
        child1.entries = KData.shared().books

        let child2 = UIStoryboard.init(name: "ParentStoryboard", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
        child2.pageTitle = "TAB 2"
        child2.entries = KData.shared().booksWithAuthors

        return [child1, child2]
    }
}

And the ChildViewController implementation:

class ChildViewController: UIViewController, IndicatorInfoProvider, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var pageTitle: String = ""
    var entries: [Book]? = nil

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

        print(String(format: "width is: %f", collectionView.bounds.width))

        return CGSize(width: collectionView.bounds.width, height: CGFloat(90))
    }
}
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • viewDidLoad in parent should always run before child. You can put log before `super.viewDidLoad()` to check again. Also can you check if collectionView frame is same as viewController frame? – Linh Quang Pham Jul 22 '19 at 03:08
  • @LinhQuangPham how do I check if it's the same frame? – Zbarcea Christian Jul 22 '19 at 15:44
  • @LinhQuangPham - I fixed it with removing `UICollectionView` and remade the whole screen with `UITableViewController`, made the cell and everything was working perfectly. I don't know what's going on, but I guess that somehow I didn't added the constraint for the `UICollectionView` or for the parent view or... really I don't why the `UICollectionView` didn't took the full width. – Zbarcea Christian Jul 22 '19 at 20:57
  • 2 way to check: print `self.view.frame` or use debug view hierarchy to see it. – Linh Quang Pham Jul 23 '19 at 02:54

0 Answers0