animating views when orientation changes not working - ios

I have a view controller that i want to animate my views position when orientation changes.
i have some views set in storyboard and some added in the ViewControllers viewDidLoad.
I'm overriding
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({context in
self.myView.frame = CGRectMake(0,0,200,200);
}, completion: nil)
}
the size changes but not in an animation.
I also have a button that is set in storyboard with autolayout.
And it also moves according to its constraints but the change is not animated.
Am i missing something?

Related

Programatically show the slide over master view in a UISplitViewController

When using a UISplitViewController in portrait mode on an iPad I am given a fullscreen view of my detail view controller which is intended. I can slide from the left and reveal the master view slide over, how can I trigger this slide over programmatically?
There's no easy way to do this it seems, but I have found that using the following code has the intended behaviour:
UIView.animate(withDuration: 0.2, animations: {
self.splitViewController?.preferredDisplayMode = .primaryOverlay
})
Make sure you set the display mode back to automatic on landscape rotation so that it will always show the master and detail like default:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { _ in
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
self.splitViewController?.preferredDisplayMode = .automatic
}
}, completion: nil)
}

ViewWillTransitionToSize don't works if used in two classes

I want to detect the orientation change in different ViewControllers. I used this code
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
print("roration detected- ViewController 1")
}
It worked fine until I added this code in another ViewController. Now the function is executed only in the second ViewController. If I deleted from there than it start working in the first ViewController again. Do you have an idea what the problem is?
You should try to call super method:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
print("rotation detected- ViewController 1")
}

IOS viewwilltransitiontosize method call all viewcontroller when rotate

My application has Tabbar and Navigation options. My problem is, when device is rotating to landscape, previous viewcontroller viewwilltransitiontosize is also calling
Let's explain the scenario,
The first screen is AssignmentViewController
The second screen is SubmissionListViewController
Both class I have override viewwilltransitiontosize method. when I rotate to landscape in the 2nd view controller time, it first calls AssignmentViewController's viewwilltransitiontosize method then it calls SubmissionListViewController's viewwilltransitiontosize
here is my piece of code
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.landscapeVideo()
self.tabBarController!.tabBar.hidden = true
} else {
print("Portrait")
self.navigationController?.setNavigationBarHidden(false, animated: false)
self.videoinPortraitMode()
self.tabBarController!.tabBar.hidden = false
}
}
any idea or help please?

showAnnotations when rotation changes

I have a SplitViewController with a mapview on as the detailViewController.
I add multiple annotations to the mapview. After adding them I call the showAnnotations:animated: method to zoom in/out the map so that all the annotations are shown in the visible portion of the map.
mapView.showAnnotations(mapView.annotations, animated: true)
Now I need the map to refocus itself when the orientation changes because the mapview's visible area reduces when you turn the iPad to portrait. So I call the same showAnnotations:animated: method in viewWillTransitionToSize:withTransitionCoordinator: method which fires when the orientation changes. But it doesn't work as expected.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
mapView.showAnnotations(mapView.annotations, animated: true)
}
How do I make it refocus when orientation changes?
Demo project
Calling the showAnnotations within the viewDidLayoutSubviews method works perfectly.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
mapView.showAnnotations(mapView.annotations, animated: true)
}

viewWillTransitionToSize is invoked on all the view controllers

The viewWillTransitionToSize method is invoked on device orientation. The problem is this is invoked on all the view controllers .
If view controller A initiates view controller B with push segue and then the device is rotated, the viewWillTransitionToSize method from A is also invoked. How to disable this?
You need to put in
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
handleLevelOrientation()
inside your viewWillTransitionToSize function. Here is my code:
override func viewWillTransitionToSize(
size: CGSize,
withTransitionCoordinator
coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
handleLevelOrientation()
}
Just like in viewDidLoad you need to put in a super, so that viewWillTransitionToSize function is not called from all of your view controllers.

Resources