处理自定义SplitViewController中的方向更改?

时间:2012-08-23 09:45:19

标签: ios uiviewcontroller uiinterfaceorientation

我编写了一个自定义SplitViewController,它有一个主人(自定义UITableView)和一个详细视图控制器,这在iPad的横向模式下运行良好。

当方向更改为纵向模式时,我想相应地调整主视图和细节视图的大小和位置,以使事物看起来不合适。

任何人都知道如何完成这项工作?

更新:

我在我的自定义SplitViewController中编写了一个监听器,以查找方向更改并将其连接到选择器(resetViews :),看起来像这样

- (无效)resetViews {

 if(currentOrientation == UIDeviceOrientationPortrait || currentOrientation ==       UIDeviceOrientationPortraitUpsideDown){

    self.view.frame = CGRectMake(0, 0, 768, 1024);
    masterView.frame = CGRectMake(MASTER_VIEW_PORTRAIT_X,MASTER_VIEW_PORTRAIT_Y,MASTER_VIEW_WIDTH_PORTRAIT,MASTER_VIEW_HEIGHT_PORTAIT);
    detailView.frame = CGRectMake(DETAIL_VIEW_PORTRAIT_X, DETAIL_VIEW_PORTRAIT_Y, DETAIL_VIEW_WIDTH_PORTRAIT, DETAIL_VIEW_HEIGHT_PORTAIT);



}
if(currentOrientation == UIDeviceOrientationLandscapeRight || currentOrientation == UIDeviceOrientationLandscapeLeft){
    NSLog(@"IN Landscape");
    self.view.frame = CGRectMake(0, 0, 1024, 768);
   masterView.frame = CGRectMake(MASTER_VIEW_LANDSCAPE_X,MASTER_VIEW_LANDSCAPE_Y,MASTER_VIEW_WIDTH_LANDSCAPE,MASTER_VIEW_HEIGHT_LANDSCAPE);
    detailView.frame = CGRectMake(DETAIL_VIEW_LANDSCAPE_X,DETAIL_VIEW_LANDSCAPE_Y, DETAIL_VIEW_WIDTH_LANDSCAPE, DETAIL_VIEW_HEIGHT_LANDSCAPE);


}

}

这在设置viewControllers的帧时工作正常。

但是当方向改变时,MasterViewController(UITableViewController)必须动态地改变其行的高度(tableView.rowHeight)。如何在不需要为每个方向更改重新加载整个tableView([tableView reloadData])的情况下执行此操作?

2 个答案:

答案 0 :(得分:0)

您是否尝试过设置autoresizingMasks以指定所需的视图大小调整行为?这样的东西应该与你描述的大小调整相匹配:

splitViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight;
masterViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight;
detailViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight;

答案 1 :(得分:0)

子类UISplitViewController并将splitViewController设置为该类。然后将此方法添加到UISplitViewController子类:

-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
    return NO;
}

如果您想要一种简单的方法来换出详细视图,请添加以下方法:

-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called

if (title) {
    UINavigationController *navController = [[UINavigationController alloc] init];
    [navController pushViewController:detailController animated:YES];
    detailController.title = title;

    NSArray *viewControllers=@[self.mainController.viewControllers[0],navController];
    self.mainController.viewControllers = viewControllers;
} else {
    NSArray *viewControllers=@[self.mainController.viewControllers[0],detailController];
    self.mainController.viewControllers = viewControllers;
}

}

要调用此方法,请从tableView中的主视图控制器执行以下操作:didSelectRowAtIndexPath:method

FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;

CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:@"Current Event"];