Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the height of a view after screen rotation in swift?

There is a view, which I add constraints in storyboard so it will change its size after screen rotation, then how can I get its height and width after screen rotation? I tried this in a rotation event function like this:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 
{
    println("h:\(view.bounds.size.height)")
    println("w:\(view.frame.size.width)")
}

but it only give me the size before rotation happen. I would like to get something like:

"now is landscape, height is...width is..." "now is portrait, height is ...width is..." in a rotation event function

like image 537
Georgey Avatar asked Mar 24 '15 13:03

Georgey


People also ask

How do you rotate a view in Swift?

rorateview.swiftlet bitmap: CGContext = UIGraphicsGetCurrentContext()! //Move the origin to the middle of the image so we will rotate and scale around the center. let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!


1 Answers

The function you are using says "Will transition", that means the transition is not completed, so you can not fetch the new size.

You need to use :

dispatch_async(dispatch_get_main_queue()) { 
    println("h:\(view.bounds.size.height)")
    println("w:\(view.frame.size.width)")
             }

This code will be executed after the rotation was completed (in the main queue) and the new sizes are available.

Regards.

like image 124
crom87 Avatar answered Oct 26 '22 13:10

crom87