0

I am showing a custom UILabel in my appdelegate class when a push notification comes. It is working fine in Portrait mode but when i rotate my device to landscape mode, label is still showing in Portrait mode. How can i fix it. I have implement rotation method. But it did not worked. Thanks in advance.

My code is :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {   
CGRect frame=[[UIApplication sharedApplication] statusBarFrame];
 if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
                || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {

                 backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 32)]; 
            } else {
                backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height, 320, 44)];   
                NSLog(@"portrait");   
            }
[backgroundImageView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"alert.png"]]];
backgroundImageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];  
            [backgroundImageView addGestureRecognizer:tgr];  
[self.window addSubview:backgroundImageView];
 }![enter image description here][1]

see here

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
shivam
  • 1,148
  • 1
  • 12
  • 28

4 Answers4

1
I have done the similar kind of thing using the following code...

   - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation       duration:(NSTimeInterval)duration {

                    [self adjustUrLableViewForOrientation:toInterfaceOrientation];

                }

              -(void) adjustUrLableViewForOrientation:(UIInterfaceOrientation)orientation
                {
                 if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
                    {
                        //Ur lable portrait view
                    }
                    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
                    {

                  //Ur lable for landscape view


                    }

                }

Also you can look into this changing the lable position based on the orientation.

Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57
0

UiWindow subviews dont rotate by themselves.. you need to check for device rotation changes like this

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) 
                                             name:UIDeviceOrientationDidChangeNotification object:nil];

and then in the method..rotate your label manually based on current orientation.

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • It is going in the loop if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {} but not showing the label in landscape mode – shivam Jan 25 '13 at 07:10
  • you have to apply CGAffineTransform on the label to rotate it 90 degrees – Shubhank Jan 25 '13 at 07:13
  • CGAffineTransform not working here. The label is showing on unknown place on the screen not on the correct position. – shivam Jan 25 '13 at 07:18
  • does your label on UIAlertview or Window..? – Madhu Jan 25 '13 at 07:25
0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait ||
   interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
   interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
} 

Add this...Hope this helps..

Or add this:

- (BOOL)shouldAutorotate{
   return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
  return UIInterfaceOrientationPortrait || UIInterfaceOrientationLandscapeLeft || UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationPortraitUpsideDown ;
}
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • It is rotating and also going in rotation method. the label is showing but not on the correct position. – shivam Jan 25 '13 at 07:30
  • then u have to ensure that the label is positioned correctly where you want it when you are in landscape mode... – lakshmen Jan 25 '13 at 07:33
  • have a look at this: http://stackoverflow.com/questions/14291600/ios-trying-to-rotate-a-text-label-and-the-label-disappears – lakshmen Jan 25 '13 at 07:34
0
[backgroundImageView setTransform:CGAffineTransformMakeRotation(M_PI/2.0)];

Should work for this case

Madhu
  • 1,542
  • 1
  • 14
  • 30