在AppDelegate中以横向模式显示UILabel

时间:2013-01-25 07:02:25

标签: iphone ios objective-c xcode landscape

当推送通知到来时,我在我的appdelegate类中显示自定义UILabel。它在纵向模式下工作正常但是当我将设备旋转到横向模式时,标签仍然在纵向模式下显示。我该如何解决呢?我有实现旋转方法。但它没有奏效。提前谢谢。

我的代码是:

- (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

4 个答案:

答案 0 :(得分: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


                    }

                }

您也可以查看此changing the lable position based on the orientation

答案 1 :(得分:0)

UiWindow subviews不要自己旋转.. 你需要像这样检查设备轮换变化

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

然后在方法中..根据当前方向手动旋转标签。

答案 2 :(得分:0)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait ||
   interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
   interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
} 

添加此...希望这会有所帮助..

或者添加:

- (BOOL)shouldAutorotate{
   return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
  return UIInterfaceOrientationPortrait || UIInterfaceOrientationLandscapeLeft || UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationPortraitUpsideDown ;
}

答案 3 :(得分:0)

[backgroundImageView setTransform:CGAffineTransformMakeRotation(M_PI/2.0)];

应适用于此案例