2013-01-25 23 views
0

當推送通知出現時,我在我的appdelegate類中顯示自定義UILabel。它在肖像模式下工作正常,但是當我將設備旋轉到橫向模式時,標籤仍然以肖像模式顯示。我如何解決它。我已經實現了旋轉方法。但它沒有奏效。提前致謝。在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

回答

0

UiWindow subviews不自行旋轉.. 你需要檢查這樣

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

設備旋轉的變化,然後在方法..根據當前的方向手動旋轉您的標籤。

+0

它在環去工作,如果([[UIApplication的sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication的sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight){}但在橫向模式下不顯示標籤 – shivam

+0

您必須在標籤上應用CGAffineTransform才能將其旋轉90度 – Shubhank

+0

CGAffineTransform在此處不起作用。標籤顯示在屏幕上的未知位置,而不是正確的位置。 – shivam

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 ; 
} 
+0

爲什麼不使用return yes? – yebw

+0

它正在旋轉,也正在旋轉的方法。標籤顯示,但不在正確的位置。 – shivam

+0

然後你必須確保標籤正確定位在你想要的地方,當你在橫向模式... – lakesh

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

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

應該針對這種情況

+0

不工作。您可以通過在willRotate方法中執行此操作來檢查此問題。 – shivam

相關問題