2013-09-30 14 views
0

我已經試過這樣:搬家標籤中心新點時,方向改變

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self adjustViewsForOrientation:toInterfaceOrientation]; 
} 
- (void)adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { 
    intervalLabel.center = CGPointMake(100, 100); 
} 
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     intervalLabel.center = CGPointMake(200, 200); 

    } 
} 

但沒有奏效。但是,如果我這樣做,將移動它

- (IBAction)sup:(id)sender { 
    intervalLabel.center = CGPointMake(100, 100); 
} 

BTW該標籤在故事板製成,並outleted我的看法沒有負載,並設爲(弱,非原子)和按鈕在也做了故事板和僅僅是一個標準的IBAction爲

+0

不清楚你想要做什麼 - 你將中心設置爲0,0,然後立即調用moveLabel將其設置爲100,100。你只是在風景方向上調用它,而不是在肖像方向上調用任何東西。所以這個標籤不應該移動 – ksimons

+0

k我改變了它所以它應該是正確的,但是當設備旋轉時仍然標籤不移動 –

回答

0

的一個問題是你不叫:

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

不過,我猜真正的問題是,你正在使用自動佈局(無論是有意或無意)和然後t試圖與這個並不真正工作的中心混爲一談。

所以我的建議 - 如果你喜歡使用設置框架和中心的「舊方法」(在可視化設計器中點擊你的UIViewController並取消選中「使用Autolayout」),或者禁用自動佈局,或者嘗試使用自動佈局(意思是在旋轉時添加/更改約束)。

取消選中「使用自動佈局」我在故事板編輯器的UIViewController後,接下來的工作對我來說:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
     _intervalLabel.center = CGPointMake(0, 0); 
    } else { 
     _intervalLabel.center = CGPointMake(50, 50); 
    }  
} 

,使用自動佈局將是我的建議是說。

+0

Thankyou,就是這樣,雖然在故事板中有辦法做到這一點嗎? –

+0

當然。只需在故事板編輯器中爲您的標籤製作一些約束,並將它們連接到視圖控制器中的插座。然後修改旋轉的約束(例如,通過設置它們的'常量'屬性。) – ksimons