1

我正在尝试在滚动视图(由段控件触发)内的两个部分(视图)之间切换可见性,问题是我不想要隐藏元素所在的间隙。

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

这是我的代码示例

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;
}

我没有直接使用滚动视图,我也尝试过使用静态UITableView并隐藏单元格,但我再次在界面中留下了空白,这就是我尝试这种方法的原因。有什么建议么?是否有更简单的方法来实现这一点?

4

1 回答 1

1

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

#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];
    }
}
于 2013-02-12T22:49:37.160 回答