VFL with Size-Class-Specific Layout dynamic with orientation support

83 Views Asked by At

I have a navigation bar added programatically to the view with VFL (visual format language)

like this

navBar.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(navBar)
let views = ["bar": navBar]
var constraints = [NSLayoutConstraint]()
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[bar]|", options: [], metrics: nil, views:views)
    constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[bar(64)]", options: [], metrics: nil, views:views)
NSLayoutConstraint.activate(constraints)

the above code works fine but gives a constant height to the navigation bar as 64. but I would like to have it based on the mobile's orientation, portrait-64 and landscape-44

1

There are 1 best solutions below

0
On

If you want to change height depending on orientation change you need to grab when orientation change

Below method will help you do this.So change your layout depending on orientation in this method too.

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.currentDevice().orientation.isLandscape.boolValue {
            print("Landscape")
            //Change your code here for landscape
        } else {
            print("Portrait")
            //Change your code here for portrait
        }
    }

Swift 3 version:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)

    }