Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Child View Controllers in UIContainerView swift ios

I am creating an app in which i have to open 2-3 view controller inside a view controller because I want to share same Sliding Drawer and Navigation bar for these view controllers. I have followed this tutorial.

I have used ContainerView inside MainController and child controllers are added properly but I am having difficulty in resizing the child controller to match containerview

 self.mainContainer.addSubview(vc.view)
 self.mainContainer.translatesAutoresizingMaskIntoConstraints = false
 addChildViewController(vc)
 NSLayoutConstraint.activate([
 vc.view.leadingAnchor.constraint(equalTo:mainContainer.leadingAnchor),
 vc.view.trailingAnchor.constraint(equalTo: mainContainer.trailingAnchor),
 vc.view.topAnchor.constraint(equalTo: mainContainer.topAnchor),
 vc.view.bottomAnchor.constraint(equalTo: mainContainer.bottomAnchor)
 ])
vc.didMove(toParentViewController: self)

and I am getting the following error

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x170095c70 h=-&- v=-&- UIView:0x127e17630.midY == UIView:0x127e15070.midY + 32   (active)>",
    "<NSLayoutConstraint:0x1740970c0 V:|-(0)-[UIView:0x127e17630]   (active, names: '|':UIView:0x127e15070 )>",
    "<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom   (active)>

what I think is self.mainContainer.translatesAutoresizingMaskIntoConstraints = false is somehow not working Child View Controllers are not properly getting resized as in the main controller the navbar is about 64 and the same height of portion is getting clipped in childview controllers from bottom.

MainController's elements Constraint

  1. navbar left=top=right = 0, height = 64

  2. UIContainerView left=right=bottom=0 and top to navbar = 0

like image 264
Sahil Avatar asked Aug 11 '17 05:08

Sahil


2 Answers

Instead of applying constraints to vc's view, you can simply set the frame of vc, i.e.

    vc.view.frame = self.containerView.bounds //Here
    self.containerView.addSubview(vc.view)
    self.addChildViewController(vc)
    vc.didMove(toParentViewController: self)
like image 93
PGDev Avatar answered Oct 02 '22 15:10

PGDev


vc.view.translatesAutoresizingMaskIntoConstraints = false

like image 30
Zhang Avatar answered Oct 02 '22 16:10

Zhang