iPhone simulator doesn't update last frame and the UIView width doesn't fill the screen

139 Views Asked by At

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))
    }
}
0

There are 0 best solutions below