2014-03-19 32 views
0

我創建UITableViewCell,並且當用戶將風景改變爲縱向或縱向時,我試圖檢查相同的代碼,但不自動檢測。如何在設備更改時立即檢測UIInterfaceOrientation方向(人像,風景)

- CellForRowAtIndexPath 
{ 
    if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     [Button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 50, 20, 50, 50)]; 
    }else 
    { 
     [Button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.height - 70, 20, 50, 50)]; 
    } 
} 

回答

1

一種方法是把這些東西加到你的viewController:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    // will rotate 
} 
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    // will animate rotation 
} 
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    // done rotating 
} 

或者,您可以註冊通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(orientationDidChange:) 
              name:UIDeviceOrientationDidChangeNotification 
              object:self]; 
+0

它非常有幫助,我使用didRotateFromInterfaceOrientation:但我必須設置[ self.tableview reloaddata]在此方法中更改位置 – user3377250

+0

請注意設備方向和界面方向之間的差異 – atreat