我正在開發一個iPad應用程序,允許用戶選擇「小部件」以顯示在屏幕上。屏幕分爲兩列,小部件將放置在左側或右側列中。每個小部件都是一個視圖控制器,具有固定寬度的靜態但取決於小部件內容的高度不同。我正在尋找一種方法將窗口小部件放置在屏幕上,以便它們自動放置在下一個邏輯位置 - 類似於Java中的佈局管理器。自動將視圖置於下一個邏輯位置(如佈局管理器)
我創建了方法addWidget,它跟蹤每列中所有小部件的高度。然後將小部件添加到最小高度的列中。這作品,但我正在尋找一個更優雅的方式來做到這一點,或想法改善目前的解決方案。
- (void) addWidget:(UIViewController *)controller {
if(leftHeight <= rightHeight) {
controller.view.frame = CGRectMake(20.0, leftHeight, controller.view.bounds.size.width, controller.view.bounds.size.height);
leftHeight = leftHeight + controller.view.bounds.size.height + 20;
}
else {
controller.view.frame = CGRectMake(self.view.bounds.size.width-controller.view.bounds.size.width-20, rightHeight, controller.view.bounds.size.width, controller.view.bounds.size.height);
rightHeight = rightHeight + controller.view.bounds.size.height + 20;
}
[scrollView addSubview:controller.view];
}
感謝