IPhone - Not getting Interface Orientation - iphone

I'm trying to set another position for a UIButton when I change the orientation of my IPhone and I had the idea to implement it inside BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation.
Is this the right way to implement or is there any error on my code? It's not printing these logs.
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ((interfaceOrientation == UIDeviceOrientationLandscapeLeft) ||
(interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
[adButton setFrame:CGRectMake(100, 700, 768, 90)];
}
if ((interfaceOrientation == UIDeviceOrientationPortrait) ||
(interfaceOrientation == UIDeviceOrientationPortraitUpsideDown))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: Portrait or UpsideDown portrait");
[adButton setFrame:CGRectMake(0, 0, 768, 90)];
}
return YES;
}
Regards!

Your code too should work but try putting following code. Change is from UIDeviceOrientation to UIInterfaceOrientation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
[adButton setFrame:CGRectMake(100, 700, 768, 90)];
}
if ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: Portrait or UpsideDown portrait");
[adButton setFrame:CGRectMake(0, 0, 768, 90)];
}
return YES;
}

shouldAutorotateToInterfaceOrientation simply specifies what rotations are supported by a UIViewController - nothing more.
You may consider modifying willRotateToInterfaceOrientation which is called just before a rotation is about to begin - to hide background views or other objects, for example.
Then, override didRotateFromInterfaceOrientation which is called after a rotation has just finished - I think this one will be the most important for your setFrame: code.

I don't undestand, the adButton setFrame works correctly or not and this viewcontroller is root or you have another one. Can't it be that root controller shouldAutorotateToInterfaceOrientation return NO for any orientation?

Related

Device rotaion issue

I detect screen change orientation with :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
and when it call i call this method:
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}else{
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
and in this function i do this stuff:
-(void)changeToBigNowPlaying:(BOOL)flag withLayer:(AVPlayerLayer*)layer{
if (flag) {
//To portrait
[landVC.view removeFromSuperview];
[layer removeFromSuperlayer];
[self.window addSubview:tab.view];
}else {
//To landscape
[tab.view removeFromSuperview];
[landVC setUpNewLayer:layer];
[self.window addSubview:landVC.view];
}
}
and this is the explanation :
I have tabbar with viewcontroller,and when i detect that the user rotate the screen to landscape i remove the tabbar and add a UIViewController(landVC) and when i rotate it to portrait again i remove the landvc and add again the tabbar.
my problem is when i rotate first to UIInterfaceOrientationLandscapeLeft so it show me the screen in UIInterfaceOrientationLandscapeRight. but when i rotate to UIInterfaceOrientationLandscapeRight first i see the screen like it should and when i rotate from this situation to UIInterfaceOrientationLandscapeLeft i see the screen good too.
any idea why i have a problem when i rotate to UIInterfaceOrientationLandscapeLeft and i see the landVc in UIInterfaceOrientationLandscapeRight
In landVC UIViewController i implement:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight){
return YES;
}else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return YES;
}else {
return NO;
}
}
Edit
-(void)detectOrientation{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
}

Control rotation

I have an app like pulse app, an horizaontal tableview. I want to show 5 rows in a section but i want to control the width of the row when it is in landscape or portrait, so it adjust to the screen. how can i do this is there a method to do that ?
Resize your UITableView when the device is rotated. Something like this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
//Resize your UITableView, i.e.:
orientation = self.interfaceOrientation;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
myTable.frame = CGRectMake(x, y, width, height);
}
else
{
myTable.frame = CGRectMake(x, y, width, height);
}
[[self view] setNeedsDisplay];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

How to manage application when orientation changes?

I have an application in which I am changing screen orientation. But when I change orientation for the first screen and go to the next screen, the change does not persist.
Attached are images for clearer understanding.
The main problem is that when device is rotated then first screen rotates but second does not launch in the new orientation.
It will rotate only when we change orientation after the screen is launched.
How do I fix that?
The problem is that when you rotate the device then
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Method gets called but when you go to the next screen then this method does not get called. if you rotate your device again then that method gets called.
So if you want to change next screen orientation as device current orientation, then check the device's current orientation with viewDidLoad method or ViewWillApper() method. Then according to that set your current view.
Use this buddy...
If suppose, you are adding UIImageView to your self.view as
**in .h**,
UIImageView *topView
**in .m**,
topView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#“anyImage.png"]];
topView.userInteractionEnabled = YES;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
topView.frame = // set your dimensions as per landscape view
}
else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
topView.frame = // set your dimensions as per portrait view
}
[self.view addSubView : topView];
**Add this lines at end of your viewDidLoad.**
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
**And the method**
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
switch (orientation)
{
case UIInterfaceOrientationLandscapeLeft:
{
topView.frame = // set your dimensions as per landscape view
break;
}
case UIInterfaceOrientationLandscapeRight:
{
topView.frame = // set your dimensions as per landscape view
break;
}
case UIInterfaceOrientationPortraitUpsideDown:
{
topView.frame = // set your dimensions as per portrait view
break;
}
case UIInterfaceOrientationPortrait:
{
topView.frame = // set your dimensions as per portrait view
break;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
Happy coding..
call this method.
-(void) viewWillAppear:(BOOL)animated{
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
[self willAnimateRotationToInterfaceOrientation:orientation duration:0];
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
enter code here
set frame.........
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
set frame.........
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}

How to set the device orientation in iphone?

I am doing a project which supports both landscape mode and portrait mode,but now i am struck there is no orentation in my project.I set the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
//return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
return YES;
}
but no use,when i press the command +Right-arrow for rotation to right it rotates to left but view and controllers didnt change to that orentation.Then after some googling i get this code
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
// do stuff
}
But the problem is i didn't know how to write code in //do stuff.Please help me to do this.
thanks in advance.
EDIT:
i put this code ,it is working but whenthe simulator is again turns to portrait it wont be in the default mode.my code is
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
//
table.transform = CGAffineTransformIdentity;
table.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
table.bounds = CGRectMake(0.0, 0.0, 320, 402);//[self.view setFrame:CGRectMake(0.0, 0.0, 768, 90)];
}
else
{
table.transform = CGAffineTransformIdentity;
table.transform = CGAffineTransformMakeRotation(degreesToRadian(360));
table.bounds = CGRectMake(0.0, 51, 320, 402);
}
return YES;
}*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
//Handle portrait
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
//Handle landscape
}
}
This is better code.
You can do like this.
if (UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation || UIInterfaceOrientationLandscapeRight == interfaceOrientation ||
UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) {
return YES;
}

Labels not aligning properly in landscape mode

Is the code below correct? When the user rotates the device, two labels are supposed to go to the coordinates given below. It works when the user starts the app in portrait mode, the labels are placed correctly. However, when the user starts in landscape mode, the labels DO NOT get placed correctly. But if you rotate the view to portrait and then back to landscape, they align properly. I've tried placing the landscape coordinates in viewDidLoad, and it still doesn't work. What should I do? Thanks for your help!
The two labels are recordingTimeLabel and recordingTimeLabelMinutes.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
// fixedSpace.width = 400;
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
Additionally, this code doesn't work either:
-(void)viewWillAppear:(BOOL)animated {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
willRotateToInterfaceOrientation: may not be called if you start in landscape mode. I suggest setting the coordinates in viewWillAppear:, not viewDidLoad, to figure out the initial orientation (you can use self.interfaceOrientation if you have autorotation enabled).
Have you set following code also for the UIInterfaceOrientation?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Got it. I used an NSTimer and called a function very often that contained this code:
-(void)updateLabelLocation {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
}
}