Currently i am working on IOS 7 and my problem is i am not able to get orientation of current device in ViewDidLoad method.
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAll);
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
NSLog(#"i am in portrait mode");
}
}
I write this code in my FirstViewController.m,and when i run my application no condition become true out of them,
can anybody help me why no condition become true even i run my application in portrait mode.
Thanks in advance.
Try this.
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsLandscape(orientation)) {
//Landscape mode
}
else
{
//Portrait mode
}
EDIT : Use UIInterfaceOrientationIsLandscape and UIInterfaceOrientationIsPortrait method to find orientation mode
try this
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
oky u hav other orientation call back methods this one called before rotation is going to occur - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration and this one just after second one - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation, for example
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation orientation = toInterfaceOrientation; //hear toInterfaceOrientation contains the which orientation is device is turning to
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation orientation = fromInterfaceOrientation; // hear fromInterfaceOrientation contains previous state of the orientation
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
}
for more information see docs
hope this helps u .. :)
Related
i create a custom button and label in portrait mode and landscape mode separately as below:
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
[self addButton1];
[self addLabel1];
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
[self addButton2];
[self addLabel2];
}
return YES;
}
if i run this code,in landscape mode both buttons and labels are displayed.
if i try this:
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
[self addButton2];
[self addLabel2];
[custombtn1 setHidden:YES];
}
error:use of undeclared identifier.
can somebody help me?
You need to implement the method:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
This method is called regardless of whether your code performs one-step or two-step rotations.
Parameters
fromInterfaceOrientation
The old orientation of the user interface. For possible values, see UIInterfaceOrientation.
Additionally, you could hide unnecessary buttons.
addButton2.hidden = YES;
after browsing through stackoverflow for answers I decided to ask a question.
From my understanding, I'm supposed to override the supportedInterfaceOrientation to handle orientation. For example's sake I implemented it like this
- (NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
This lets the controller start in landscape mode when presented and get the forceLandscape ON on default. Then there's a button that will change the orientation on button press
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
UIInterfaceOrientation o = UIInterfaceOrientationPortrait;
if (self.forceLandscape) {
o = UIInterfaceOrientationLandscape;
}
[UIApplication sharedApplication].statusBarOrientation = o;
}
Button press would alternatively change between portrait and landscape mode. By setting the status bar orientation it would call the supportedInterfaceOrientations to change the orientation for me. It does call the method and return mask portrait on first button press but it doesn't change the orientation for me. This is the issue I want to fix. Hope that there's a workaround for this.
Replacing the status bar orientation change to this code
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: o] forKey:#"orientation"];
Does call supportedInterfaceMethod and it does change the orientation. However it only work once and that it has access to private code and will be rejected by Apple, which is not desirable.
Not sure that this solution works. In my project (iOS6,7), I fixe the orientation so I don't need to force to change the orientation. However, I found a function in UIViewController that "attemp to rotate the device orientation to the right one
-(BOOL)shouldAutorotate {
FLog(#"");
if (self.forceLandscape) { //force to landscape
return NO;
} else {
return YES; //let's application rotate it self
}
}
-(NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscapeLeft;
} else {
//You can just allow Portrait.
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
}
}
// Notifies when rotation begins, reaches halfway point and ends.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//save new orientation
_myOrientation = toInterfaceOrientation;
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
if (self.forceLandscape) {
_myOrientation = UIInterfaceOrientationMaskLandscapeLeft
} else {
//just do nothing
}
//call this to update orientation
[UIViewController attemptRotationToDeviceOrientation];
}
My Application supports all orientation but in few cases i want to lock the screen in
portrait/landscape mode.
In my Application i have two kind of pdf document.
one is in portrait document and other is landscape document .
i want to open portrait document in portrait view only, and landscape document in
landscape view only.
i want to do like this: if my application is open in landscape view and i click on the
portrait document so it must rotate in portrait view and same like this for landscape if
my application is open in portrait view and when i click on the landscape document it
must be rotate or it must open the document in landscape only.
hope i make you guys clear pardon my english hope you understand what i want please
need your help .
Thank you in advance
here is my some code :
- (void)viewDidLoad
{
[super viewDidLoad];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation ==
UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"Portrait");
if ([orientationObject isEqualToString:#"p"]) {
//If the document is portrait
pdfScrollViewFrame = CGRectMake(-0.0, -80.0, 770.0, 1085.0);
}
else{
// If the document is landscape
pdfScrollViewFrame = CGRectMake(-0.0, -40.0, 770.0, 1130.0);
}
}
else{
NSLog(#"Landscape");
if ([orientationObject isEqualToString:#"p"]) {
//If the document is portrait
pdfScrollViewFrame = CGRectMake(65.0, -80.0, 620.0, 1110.0);
}
else{
//if the document is landscape
pdfScrollViewFrame = CGRectMake(0.0, -40.0, 740.0, 1070.0);
}
}
For iOS6+, you could add this to your controller:
- (BOOL)shouldAutorotate {
YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (<PDF is portrait>)
return UIInterfaceOrientationMaskPortrait;
if (<PDF is landscape>)
return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskAll;
}
Hope this helps.
EDIT:
I am not sure if you need to manually rotate the PDF to get the results you want. In this case you might try with something like:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration {
if (toOrientation == UIInterfaceOrientationMaskLandscape)
pdfScrollViewFrame.transform = CGAffineTransformMakeRotation(M_PI/2);
… // handle all other cases here
}
In order to only lock rotation after viewDidAppear, I would do following:
#interface…
…
#property (nonatomic) BOOL isRotationLocked;
…
#end
#implementation…
…
- (void)viewDidAppear:(BOOL)animated {
…
self.isRotationLocked = YES;
}
- (BOOL)shouldAutorotate {
YES;
}
- (NSUInteger)supportedInterfaceOrientations {
if (self.isRotationLocked) {
if (<PDF is portrait>)
return UIInterfaceOrientationMaskPortrait;
if (<PDF is landscape>)
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAll;
}
…
I have an application that supports only landscape mode. When I opening the camera the app crashes. I solved the problem by using the following code.
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
But the app turns to support all orientations. All I want is the app only support landscape mode and also uses camera.
Any help will be appreciated.
I finally figured it out. The shouldAutorotate never gets called in ViewControllers. Because the topmost view controller has to return the interface orientations it wants to rotate to. In my case, that's the UINavigationController. So I creates the new category for UINavigationController to make it work. I uses the following methods in the category
#implementation UINavigationController (BugiOSFix)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
#end
Write this For orientations .........
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:
[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation =
[UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
// Write here for left and right orientation
}
else if(is_iPhone)
{
// Write here for iPhone land scape ;
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
// Write here for Portrait orientation for iPad
}
else if(is_iPhone)
{
// Write here for iPhone Portrait orientation ;
}
}
See my answer at How to create a landscape-view only ios application for a similar question. All you have to do is to implement one method and your app will be forced to autorotate to the direction you want it.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
This will force your app to rotate to the UIInterfaceOrientationLandscapeLeft direction by providing your ViewController with only one preferred orientation.
I check the orientation of the device in viewDidAppear and viewWillAppear and force the orientation by calling willAnimateRotationToInterfaceOrientation method.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
_levelComplete = YES;
[self willAnimateRotationToInterfaceOrientation:[[UIDevice currentDevice] orientation] duration:0.01];
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight) )
{
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
}
}
The problem i face is that toInterfaceOrientation is remains 0 for both viewDidAppear and viewWillAppear method hence program crashes.
What might be the problem?
Please Help!
Try this
- (void) viewDidLoad
{
_levelComplete = YES;
[self adjustViewsForOrientation:self.interfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
As you mentioned : The problem i face is that toInterfaceOrientation is remains 0 for both viewDidAppear and viewWillAppear method hence program crashes.
The orientation 0 refers to unknown orientation. Set the break point on viewWillAppear and viewDiddAppear and on orientation delegates. Note that do the delagates of orientation gets called first of the view delegates.
You can use the delegate
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Using this delegate and the application will be present in Landscape mode or any desired orientation mode.
Try it.