Child view container don't match to desired width in swift

740 Views Asked by At

I have a view container to show only 1 screen with phone and 2 with tab. So, in ViewDidLoad(), I use a the method

 func addListController(){
    self.addChildViewController(listController!)
    listController?.view.frame = defineListSize()
    self.containerView.addSubview((listController?.view)!)
    listController?.didMove(toParentViewController: self)
}

where the method defineListSize determine the size of the first children

 func defineListSize() -> CGRect {
    var rect :CGRect = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width, height: self.containerView.frame.size.height)

    switch (deviceIdiom) {
    case .tv:
        rect = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width/2, height: self.containerView.frame.size.height)
    case .pad:
        rect = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width/2, height: self.containerView.frame.size.height)
    case .phone:
        rect = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width, height: self.containerView.frame.size.height)
    case .unspecified: break
    default: break
    }

    return rect
}

However, when the first children is added on tablet, it is always widder than the half of the container view. Although, in debug, I can see that rect has the good data. I tried to use addListController() in viewWillAppear or viewWillLayoutSubviews(), but same result. The second child view, when added on the right side, has the good dimensions, and cut the right part of the first child.

So where is the problem? Thanks

0

There are 0 best solutions below