我有一個包含兩個表的視圖。在故事板中,我有兩個單獨的視圖,一個是水平的,另一個是垂直的。當我需要導航到該視圖,代碼檢測的方向,並提出了相應的視圖(和改變方向時這樣做iOS在方向更改期間調整UITableView的尺寸
我有我的方法,下面的代碼:
-(void)viewDidAppear:(BOOL)animated{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
if(tableHeight2 > 324){
tableHeight2 =325;
}
table1.frame = CGRectMake(table1.frame.origin.x, table1.frame.origin.y, table1.frame.size.width, tableHeight1);
table2.frame = CGRectMake(table2.frame.origin.x, table1.frame.origin.y + 20 + tableHeight1, table2.frame.size.width, tableHeight2);
}else {
if(tableHeight2 > 500){
tableHeight2 = 500;
}
table1.frame = CGRectMake(table1.frame.origin.x, table1.frame.origin.y, table1.frame.size.width, tableHeight1);
table2.frame = CGRectMake(table2.frame.origin.x, table1.frame.origin.y + 50 + tableHeight1, table2.frame.size.width, tableHeight2);
}
}
這個功能奇妙,當我按下一個按鈕導航到視圖時,它將所有單元格高度相加,並使第一個表格適當高度,然後將第二個表格移動到第一個表格下方50像素處。確保第二張桌子不會超出可見屏幕區域。
當方向的變化,我下面的代碼被執行:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
InitViewController *ini;
ini = [self.storyboard instantiateViewControllerWithIdentifier:@"Init"];
ini.location = MenuName;
[self presentViewController:ini animated:NO completion:nil];
}
這應該做按下barbuttonitem做同樣的事情:改變InitViewController而在ini.location發送StoryboardID它變量。導航按鈕的代碼與willAnimateRotationToInterfaceOrientation中的代碼非常相似。 InitViewController然後確定方向並將應用程序發送到正確的故事板UIView。
它確實把它發送到正確的視圖,我可以告訴基於表寬度。它是什麼不是做的是改變第一個(頂部)表table1的高度。第一張桌子保留了它在故事板中的大小。
如果您認爲我需要發佈代碼以獲得更好的照片,請告訴我我很樂意添加它。任何幫助,見解,甚至只是試驗和錯誤的建議,將不勝感激。
*注:我試圖將willAnimateRotationToInterfaceOrientation更改爲ViewDidLayoutSubviews,不起作用。