1

我的 tableview 控制器中有一个自定义分段控件,tableview 的标题中有一个搜索栏。当我向上滚动到最后一个单元格时,tableview 会弹回来。但问题是:我的搜索栏隐藏在分段控件后面,视图底部出现一个空白。我希望如果用户向上滚动表格视图,它会反弹并返回到之前的位置,如第一个屏幕截图所示。

这是我的分段控制的代码:

self.projectSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Active Projects", @"All Projects", nil]];
    [self.projectSegmentControl addTarget:self action:@selector(actionSelectProject) forControlEvents:UIControlEventValueChanged];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width -20;
    self.projectSegmentControl.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-screenWidth-5,10, screenWidth, 30.0);
    _segmentControlView = [[UIView alloc] initWithFrame: CGRectMake(0,60, screenRect.size.width,47.0)];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _segmentControlView.backgroundColor = [UIColor whiteColor];
    self.projectSegmentControl.backgroundColor = [UIColor whiteColor];
    [_segmentControlView addSubview:self.projectSegmentControl];
    [appDelegate.window addSubview:_segmentControlView];
    self.projectSegmentControl.selectedSegmentIndex = 0;
    CGFloat screenHeight = screenRect.size.height;
    if(screenHeight==480 || screenHeight==568)
    {
        CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);
        [self.tableView setContentOffset:newOffset animated:YES];
    }

    UIFont *font = [UIFont fontWithName:@"Avenir" size:12.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    [_projectSegmentControl setTitleTextAttributes:attributes forState:UIControlStateNormal];

这是 tableview 出现时的屏幕截图: 在此处输入图像描述

当我向上滚动 tableview 反弹但搜索栏隐藏在分段控件后面时。

在此处输入图像描述

PS:如果需要,我会添加代码。提前致谢

4

1 回答 1

0

上面的帖子假设您正在使用

UITableViewController(不是UIViewController

您已在表格视图的标题中添加了搜索栏。

然后以编程方式创建了一个分段控制器并添加到窗口中

 CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width -20;
    self.projectSegmentControl.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-screenWidth-5,10, screenWidth, 30.0);
    _segmentControlView = [[UIView alloc] initWithFrame: CGRectMake(0,60, screenRect.size.width,47.0)];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

所以当你滚动表格时,显然标题也会随之移动。所以它隐藏在分段控件后面。

解决方案

step 1. 创建 uiviewcontroller (Not uitableviewcontroller)
step 2. 在视图中添加分段控件
step 3. 在分段控件下方添加静态搜索栏。步骤 4.so 滚动 tableview 时,分段控件不会被隐藏

希望这会帮助你。

于 2016-05-21T10:43:07.987 回答