隐藏视图而不留间隙

时间:2013-02-12 22:38:33

标签: ios objective-c ios6 uiscrollview interface-builder

我正在尝试在滚动视图中的两个部分(视图)之间切换可见性(由段控件触发),事情是我不希望隐藏元素的间隙。

目前我在做的是ViewDidAppear我正在存储两个视图框架,然后当我需要切换视图的可见性时,我正在设置视图,我想隐藏框架到{通过将其框架设置为其初始框架并修改其origin.y属性,将另一个视图移动到“顶部”位置{1}}和移动。问题是,只要我滚动视图的框架似乎就会重置,我就会留下空隙。

以下是我的代码

的示例
CGRecZero

我没有直接使用滚动视图,我也尝试使用静态if (control.selectedSegmentIndex == 0) { [self.firstView setHidden:YES]; [self.firstView setFrame:CGRectZero]; [self.secondView setHidden:NO]; CGRect frmTmp = secondViewRec; frmTmp.origin.y = orgPoint.y; [self.secondView setFrame:frmTmp]; } else { [self.secondView setHidden:YES]; self.secondView.frame = CGRectZero; [self.firstView setHidden:NO]; CGRect frmTmp = firstViewRec; firstViewRec.origin.y = orgPoint.y; self.firstView.frame = frmTmp; } 并隐藏单元格,但我再次留下了界面中的空白,这就是我为什么要尝试这种方法。 有什么建议?是否有更简单的方法来实现它?

1 个答案:

答案 0 :(得分:1)

我解决这个问题的方法是在父视图中附加和删除视图。我的父视图是一个scrollview。这是我的代码的一些黑客复制粘贴:

#define LIST_BUTTON_INDEX      0
#define LOCATION_BUTTON_INDEX  1
#define ACTIVITIES_DETAIL_MODE_DETAILS   0
#define ACTIVITIES_DETAIL_MODE_MAP       1

-(void)detailsButtonPressed:(id)sender
{
    [self selectButton:LIST_BUTTON_INDEX];   // change the segment control
    [self displayView:ACTIVITIES_DETAIL_MODE_DETAILS];
}
-(void)mapButtonPressed:(id)sender
{
    [self selectButton:LOCATION_BUTTON_INDEX];  // change the segment control
    [self displayView:ACTIVITIES_DETAIL_MODE_MAP];
}


-(void) displayView:(int)displayMode
{
    self.mode = displayMode;

    switch (self.mode)
    {
        case ACTIVITIES_DETAIL_MODE_DETAILS:
        {
            [self clearScrollViewSubviews];

            self.detailsController = [[ActivityDetailsView alloc] initWithNibName:@"ActivityDetailsView" bundle:[NSBundle mainBundle]];
            [self.scrollView addSubview:self.detailsController.view];
            CGRect f = self.detailsController.view.frame;
            f.size.height  -= 20;
            self.scrollView.contentSize = f.size;
        }
        break;

        case ACTIVITIES_DETAIL_MODE_MAP:
        {
            [self clearScrollViewSubviews];
            self.mapController = [[ActivityMap alloc] initWithNibName:@"ActivityDetailsLocationView" bundle:[NSBundle mainBundle]];
            [self.scrollView addSubview:self.mapController.view];
            CGRect f = self.mapController.view.frame;
            f.size.height  -= 20;
            self.scrollView.contentSize = f.size;
        }
        break;
    }

}
-(void)clearScrollViewSubviews
{
    if(self.detailsController != nil)
    {
        [self.detailsController.view removeFromSuperview];
    }
    if(self.mapController != nil)
    {
        [self.mapController.view removeFromSuperview];
    }
}