Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?
Okay found it, just have to use the animateAlongsideTransition:completion:
method on the passed UIViewControllerTransitionCoordinator
.
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// do whatever
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
Is there a non-deprecated method that matches didRotateFromInterfaceOrientation
As of iOS 8, all UIViewControllers inherit the UIContentContainer protocol, one of whose methods is - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
, which you can (simplistically) override like this:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in willRotateToInterfaceOrientation would go here.
// If you don't need anything special, you can set this block to nil.
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Stuff you used to do in didRotateFromInterfaceOrientation would go here.
// If not needed, set to nil.
}];
}
You'll notice there's nothing specific about orientation, which is by design; iOS view rotations are essentially a composition of a specific translation matrix operation (resizing a view from one aspect ratio to another is just a specific case of a general resizing operation where the source and target view sizes are known in advance) and a rotation matrix operation (which is handled by the OS).
iOS 8 Rotation Methods Deprecation - Backwards Compatibility
The rotation methods are deprecated in the iOS 8 SDK. This will have no effect at all on apps built with the iOS 7 SDK, even running in iOS 8 and probably several future versions of iOS.
As an example, the font
property of UIButton
has been deprecated since iOS 3.0 and is still available in iOS 7.0.
didRotateFromInterfaceOrientation is deprecated in iOS 8
The following should be able to handle orientation of video preview properly in swift 2.1:
override func viewWillAppear(animated: Bool) {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("deviceOrientationDidChange:"),
name: UIDeviceOrientationDidChangeNotification,
object: nil)
}
override func viewDidDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
super.viewDidDisappear(animated)
}
func deviceOrientationDidChange(sender: AnyObject) {
let deviceOrientation = UIDevice.currentDevice().orientation;
switch (deviceOrientation)
{
case .Portrait:
previewLayer.connection.videoOrientation = .Portrait
case .LandscapeLeft:
previewLayer.connection.videoOrientation = .LandscapeRight
case .LandscapeRight:
previewLayer.connection.videoOrientation = .LandscapeLeft
case .PortraitUpsideDown:
previewLayer.connection.videoOrientation = .PortraitUpsideDown
default:
break
}
}
Is there a method like didRotateFromInterfaceOrientation which happens during rotation in Swift?
Just use this code snippet to check the orientation changes
Swift 1.x/2.x
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
} else {
print("Portrait")
}
}
Swift 3.x
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
}
}
didRotateFromInterfaceOrientation firing Twice when Rotated
I have my answer. I just downloaded the simulators for iOS 7.1 for use within the latest Xcode. I found that the viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
method is NOT called when running under iOS 7.1. However, I also found that the issue I described with the rotation firing twice does NOT happen with the willRotateToInterfaceOrientation
method in iOS7 but again it DOES in iOS 8. This is a clear bug at Apple.
It looks like I will need to detect the OS version the customer is running and if it is iOS 8 or above I will not have it execute any code in the willRotateToInterfaceOrientation
method. I can, however, leave the viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
method in there for iOS 8 devices as this method will just be ignored by iOS 7.
I don't know if this is just a problem for splitviewcontrollers or for all view methods using rotation between iOS 7 and 8. If your app is not overriding this method than you'd never know. If it is you will face what I did above. Not good.
Here is the code I am using to check for version:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (NSFoundationVersionNumber == NSFoundationVersionNumber_iOS_7_1) // use this only for iOS7 devices as otherwise this fires twice under iOS8
{
...
}
}
I have left -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
method as this will simply be ignored by iOS 7 devices but will be called by iOS 8.x and presumably above.
Related Topics
How to Use Autolayout to Set Constraints on My Uiscrollview
Load Local HTML into Uiwebview Using Swift
How Does Apple Notify iOS Apps of Refunds of In-App Purchases (Iap)
How to Get Rgb Values from Uicolor
How to Send a Silent Push Notification Payload
Posting JSON Data Using Afnetworking 2.0
Does @Synchronized Guarantees for Thread Safety or Not
iOS Swiftui: Pop or Dismiss View Programmatically
Jenkins - Xcode Build Works Codesign Fails
"Do Not Embed", "Embed & Sign", "Embed Without Signing". What Are They. What They Do
Check If Optional Array Is Empty
iPhone - Draw Transparent Rectangle on Uiview to Reveal View Beneath
Avcapturesession with Multiple Previews
Setting Selected Image in Tab Bar Controller with Storyboard
Levenshtein Distance Algorithm Better Than O(N*M)
Class Has No Initializers Swift
Difference Between Dispatchqueue.Main.Async and Dispatchqueue.Main.Sync