I have two views in my app; "Calculator" and the "Tape". I can click on the button inside calculator to get to the tape and vice versa. I set the rotation as per code below and it works fine most of the time.
However there are issues if I rotate either calculator view or tape view to landscape and then if I try to access other view, interface is all messed up like it doesn't recognize that device was already rotated. Any suggestions?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self doLayoutForOrientation:toInterfaceOrientation];
}
- (void)doLayoutForOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsPortrait(orientation))
{
//set the frames here
}
else
{
//set the frames here
}
}
write these codes in the view will appear of each view.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//adjust the view for landscape
}
else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//adjust the view for portrait.
}
Related
I want to rotate my UISlider in iPhone landscape layouts and am using this code to to it.
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
However, I want to use it in -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator so that it rotates appropriately based on size classes.
I have the following code where I put other rotation code
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
self.stillCamera.outputImageOrientation = UIDeviceOrientationPortrait;
}
}
I want to make sure that the UISlider is in the proper orientation in each orientation with the left edge of the slider on top in landscape and the right on the bottom. I need to check the rotation to make sure that slider is in the right position regardless of how the user rotates from portrait to landscape left or right. How can I do this?
EDIT:
I am using GPUImage and the preview view uses an orientation left/right for that view, so I need to find an implementation that allows me to keep that while also allowing for general size class customization.
I figured it out
First - place the UISlider in a container view and pin that container view as needed.
Pin the UISlider to the container view in portrait - either centered horizontally/vertically with equal height/width to container.
In landscape, pin container view as needed with UISlider centered H/V with equal width to container.
In
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeRight;
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
self.stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
self.customSlider.transform = CGAffineTransformMakeRotation(M_PI/2);
}
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
self.stillCamera.outputImageOrientation = UIDeviceOrientationPortrait;
self.customSlider.transform = CGAffineTransformIdentity;
}
}
I have an app that works on landscape, already in the store, and decided to add a camera for a certain new feature in my next update.
with an iPad 2 the image inside the camera appears rotated 90 degrees in clockwise direction.
Once the picture is taken it appears it's saved correct position.
Needed is that always there should be a landscape mode,while taking picture and once image is clicked and saved.But image inside the camera appears to be in Portrait view. I also checked the device orientation i.e faceUp orientation.
I have used the below code to manage the orientation but could not resolve
- (BOOL)shouldAutorotate {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationFaceUp )
{
return NO;
}
else if (orientation == UIDeviceOrientationFaceDown)
{
return NO;
}
return YES;
}
My Storybuilder is designed with a portrait layout. When I start the app with my iPad already turned to horizontal, it's able to correctly detect it's in a horizontal position. But when I start the app with my iPad in a portrait position, it thinks it's in horizontal. However, every time I rotate it, the code is able to detect the correct orientation properly.
- (void) viewDidLoad
{
[self updateForOrientation];
}
- (void)updateForOrientation
{
if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) // became portrait
{
NSLog(#"is portrait");
//code for changing layout to portrait position
}
else //became horiztontal
{
NSLog(#"is horizontal");
//code for changing layout to horizontal position
}
}
Output: is horizontal (this is the output whether it starts up as portrait or landscape)
The problem is that you're sending the devices orientation in terms of the UIDeviceOrientation enum to a function that's expecting a UIInterfaceOrientation value.
If you command click on UIInterfaceOrientationIsPortrait(), you can see that it is defined as follows.
#define UIInterfaceOrientationIsPortrait(orientation) ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)
And if you look at the enum declarations for the two orientation types (documentation links below), you can see that there is a misalignment in value due to the device orientation containing a value for "none". Anyway, changing your code to use UIInterfaceOrientation should sort this out. Example:
- (void)updateForOrientation
{
UIInterfaceOrientation currentOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
NSLog(#"is portrait");
}else{
NSLog(#"is horizontal");
}
}
https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIInterfaceOrientation
https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientation
I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode
- (void)viewDidAppear:(BOOL)animated
{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
NSLog(#"orientation %d", orientation);
if ((orientation == 2) || (orientation == 3) || (orientation == 4))
{
[self performSegueWithIdentifier:#"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
}
The interfaceOrientation property is deprecated since iOS 8.
And the helpers methods
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
won't help either, because they return false when orientation is .faceUp.
So I ended checking this way:
extension UIViewController {
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or faceDown
guard let window = self.view.window else { return false }
return window.frame.size.width < window.frame.size.height
}
}
}
This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.
I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.
In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.
The most important difference for your case is that the interface orientation is never face up/down.
In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.
Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)
Bear in mind that a device orientation landscape left means a user interface orientation landscape right.
Yes you can, the UIDeviceOrientation is an enum which contains:
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
There are even two helpers:
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.
There is one way to check it.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.
I am drawing a view on the iPad that only supports landscape orientations, however sometimes in the iOS simulator the view is drawn upside down and the iPad status bar is in portrait mode. When I rotate the device in the simulator, everything behaves normally, it's just the initial load that is weird. I'm setting up my view using CGRect and use the standard iOS coordinate system.
My autorotation method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) | (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
Was that a typo in your code snippet; use "||", not "|", so:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}