Ninja
Ninja

Reputation: 358

Weird layout with tabBar.topAnchor

I have several subviews

    view.addSubview(collectionView)
    view.addSubview(pageControl)
    view.addSubview((tabBarController?.tabBar)!)
    view.addSubview(skipButton)
    view.addSubview(nextButton)

and I tried to layout by means of layoutAnchor. (at this time I anchored pageControl.bottomAnchor to view.bottomAnchor )

   nextButton.anchorWithConstantsToTop(view.topAnchor, left: nil, bottom: nil, right:
        view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
    nextButtonTopAnchor = nextButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
    nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
    nextButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
    skipButton.anchorWithConstantsToTop(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
    skipButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
    skipButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
    pageControl.anchorWithConstantsToTop(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 28, bottomConstant: 0, rightConstant: 30) 

So it works nice and when i rotate device pageControl clamps in a proper place enter image description here

But if tried to anchor pageControl.bottomAnchor = tabBarController?.tabBar.topAnchor instead of view.bottomAnchor then in portrait orientation everything work ok, but when i rotate device happens something irritating : pageControl goes upenter image description here

moreover when I get form landscape orientation to portrait orientation back it remains in upper position enter image description here

I guess the problem lays in pageControl.bottomAnchor = tabBarController?.tabBar.topAnchor string but i can get why? In landscape orientation still remains tabBar and pageControl still had to anchor it to tabBar's topAnchor. Please maybe someone do know whats going on with that behavior?

Upvotes: 0

Views: 1164

Answers (1)

Reynaldo Aguilar
Reynaldo Aguilar

Reputation: 1936

The problem lies in this line:

view.addSubview((tabBarController?.tabBar)!)

You shouldn't do that. Please remove it and then set up your constraints after the current view controller has been added to the UITabBarController. A sample candidate for this would be the first time the viewWillAppear is executed.

If you pageControl is created programmatically then you should also set its translatesAutoresizingMaskIntoConstraints property to false (as pointed out in the comments). If the control is created in a storyboard this step isn't necessary.

Upvotes: 1

Related Questions