Device orientation not work properly in iOS - ios

+(CGFloat)maxTextWidth
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
return 220.0f;
}
else
{
return 400.0f;
}
}
else
{
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
return 400.0f;
}
else
{
return 700.0f;
}
}
}
when my device in portrait orientation according to the above method it return the portrait value but the problem is that in portrait orientation sometimes it return the portrait value and sometimes it return landscape value but my device always in portrait orientation.

In your if statement check status bar orientation instead of device orientation..
+(CGFloat)maxTextWidth
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
{
return 220.0f;
}
else
{
return 400.0f;
}
}
else
{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
{
return 400.0f;
}
else
{
return 700.0f;
}
}
}

Copy this code in your viewdidload.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didrotatedevice:) name:UIDeviceOrientationDidChangeNotification object:nil];
and copy this method in your class
- (void)didrotatedevice:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(#"Landscape Left!");
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(#"Landscape Right!");
}
else
{
NSLog(#"Potrait!");
}
}
Let me know if you have any doubts?

Why you didn’t use
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Or you can use this
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Or this
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
These methods detect any orientation changes.
Set your UI changes in these methods.

Related

How to disable specific orientation in iOS

I want to disable landscape orientation in some view.
I have overridden the following two methods but these method is not going to call at any time.
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
Am I missing something?
Please help me.
Create subclass of UINavigationController and override these methods as per below:
- (NSUInteger)supportedInterfaceOrientations
{
if([self.topViewController respondsToSelector:#selector(supportedInterfaceOrientationsForThisContorller)])
{
return(NSInteger)[self.topViewController performSelector:#selector(supportedInterfaceOrientationsForThisContorller) withObject:nil];
}
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotate
{
if([self.visibleViewController respondsToSelector:#selector(shouldAutorotateNow)])
{
BOOL autoRotate = (BOOL)[self.visibleViewController
performSelector:#selector(shouldAutorotateNow)
withObject:nil];
return autoRotate;
}
return YES;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
and use below method in your UIViewController class where you want to set only portrait orientation
- (NSUInteger) supportedInterfaceOrientationsForThisContorller {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
-(BOOL)shouldAutorotateNow
{
return YES;
}
Check out this answer. The guy has probably explained the delegates in much detail.
Also try,
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Did you used below method in your code, with out this supportedInterfaceOrientations will not get called
-(BOOL) shouldAutorotate
{
return YES;
}

Orientation Working on simulator but not on device for ios7

I have seen multiple copies of the same question.but it has not supported my cause.
i have tried subclassing the navigationController adding category for the navigation controller and when i tried all these the methods shouldAutoRotate and supportedInterfaceOrientations gets invoked for Both the navigationcontroller and view controller but the device stays in portrait view only.
#import "UINavigationController+Rotation.h"
#implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate {
BOOL result = self.topViewController.shouldAutorotate;
return result;
}
- (NSUInteger)supportedInterfaceOrientations {
NSUInteger result = self.topViewController.supportedInterfaceOrientations;
return result;
}
#end
Is some thing that i do wrong?
Call this method form viewdidload:
-(void)rotateCameraView {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(deviceOrientationDidChange:) name:
UIDeviceOrientationDidChangeNotification object: nil];
}
When you rotate device this method will call automatically
-(void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp
|| orientation == UIDeviceOrientationFaceDown || orientation ==
UIDeviceOrientationUnknown) { return; }
if ((UIDeviceOrientationIsPortrait(orientation)
||UIDeviceOrientationIsPortrait(orientation)) ||
(UIDeviceOrientationIsLandscape(orientation) ||
UIDeviceOrientationIsLandscape(orientation))) {
//still saving the current orientation
currentOrientation = orientation; } [self
performSelector:#selector(orientationChangedMethod) withObject:nil
afterDelay:0];
}
-(void)orientationChangedMethod
{
switch (currentOrientation) {
case UIDeviceOrientationPortrait:
if (isOrientationEffect==YES)
{
appDelegate.imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI);
// if (!isPurchased)
// {
//
// [self buttonFrameWithIad];
// }
// else
// {
// [self buttonFrameWithoutIad];
// }
[self MakeTransformRotation];
}
[UIView beginAnimations:#"rotateView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.35f];
camDefaultImg.transform = CGAffineTransformMakeRotation(0);
[UIView commitAnimations];
break;
case UIDeviceOrientationLandscapeLeft:
if (isOrientationEffect==YES)
{
}
}

iPad iOS7 Orientation Problems

I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.
When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version
Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(#"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
Here is the notification I used:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
Here is the code I used to read the orientation change:
- (void) didRotate:(NSNotification *)notification
{
NSLog(#"Parent: %#", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.

Record video in Landscape always even device rotating (Landscape->Portrait->Landscape)

I need to record video always in landscape mode even my device rotating (Portrait->Landscape->Portrait ).
To take angle is simple, im using hyroscope. My problem is to record video in landscape always. Any advices needed.
Im using AVCaptureSession to video rec. AVMutablecomposition? I dont know what to use...
help me,please
Why not forcing your Controller to be only in landscape mode ?
You can do it like this :
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
You can try this code
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Also have look on the Apple's docs HERE for detail rotation implementations.
Try this before starting to record a video:
let videoDataOuputConnection = videoFileOutput.connection(with: .video)
videoDataOuputConnection!.videoOrientation = AVCaptureVideoOrientation.landscapeLeft

How to provide two different views in portrait and landscape?

Im currently working with a IPhone-app that has a "timetable".
in portrait i want it to have a regular table-view with some customization! When i have the IPhone in landscape i want it to change to a more "timetable"-view, with tables and rows.
Is it possible?
Try This
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//write code for portrait mode
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//write code for landscape mode
}
}
See Apples documentation Creating an Alternate Landscape Interface
Make sure to read the documentation, but here is the code from the example implementation:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Answer is Yes it's possible through the following method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
\\ your timetable customisation goes here
}
}
You also need this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

Resources