2014-02-13 38 views
3

我有一個tableview標題(不是節標題)的tableview,它們都根據方向調整大小。在我的實現中,我爲viewWillLayoutSubviews中的兩個視圖設置了框架。UITableView和UITableViewHeader在旋轉和調整大小後發生衝突

我現在的問題是,雖然兩個都在旋轉後正確單獨調整大小,但tableView的原點不會改變,以考慮到標頭的高度變化。這導致它們之間出現空間或覆蓋某些單元格的標題。

我的目標是在肖像中,tableview的屏幕寬度與一個長頭。當旋轉到橫向時,寬度會減小到屏幕寬度的三分之一,右對齊,並使用較短的標題。同樣,我需要所有這些更改的旋轉動畫平滑和合乎邏輯。

我對iOS旋轉和動畫實踐相當綠色,所以如果任何人有更好的建議,我一定會很感激他們。

回答

1

表視圖將不會尊重新的標題高度,除非您重新指定tableHeader。

類似的問題:

當旋轉將或沒有完成,你可以做到以下幾點:

- (void)didRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    self.tableView.tableHeaderView = self.tableView.tableHeaderView; 
} 

事實上,這裏也是在那裏你可以計算頭部的高度的地方:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    CGRect frame = self.tableView.tableHeaderView.frame; 
    frame.size.height = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? 50 : 150; 
    self.tableView.tableHeaderView.frame = frame; 

    self.tableView.tableHeaderView = self.tableView.tableHeaderView; 
} 
+1

嗨,重新分配頭是我失蹤。在設置標題的框架之後,我用self.tableView.tableHeaderView調用了setTableHeaderView。現在一切都很好。非常感謝。 –

+1

第一個代碼示例中的方法簽名應該是'-didRotateFromInterfaceOrientation:'而不是'-didRotateToInterfaceOrientation:duration:'。 – Frederik

0

在iOS 8.0或更高版本,你可以打電話來代替:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator; 

如果覆蓋的方法,請確保調用超級在某一點。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 



     self.tableView.tableHeaderView? = YourTableHeaderView() 

    }, completion: { (UIViewControllerTransitionCoordinatorContext) in 



    }) 

    super.viewWillTransition(to: size, with: coordinator) 
}