1

I have a custom segmented control in my tableview controller and a search bar in header of tableview. When i scroll up till last cell, tableview bounce back. But problem is: My search bar hides behind the segmented control and a white space appears on bottom of the view. I want that if user scroll up the tableview it bounce and came back to its previous position as shown in first screenshot.

Here is code of my Segmented Control :

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

It is the screenshot when tableview appears : enter image description here

When i scroll up tableview bounce back but search bar is hide behind the segmented control.

enter image description here

PS: I will add the code if needed. Thanks in advance

Muhammad Umair
  • 583
  • 11
  • 32

1 Answers1

0

with the above post am assuming that you are using

UITableViewController (Not UIViewController)

you have added the searchbar in the header of the tableview.

then programatically you have created a segmented controller and added in the window

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

so while you scrolling the table, obviously header also will move along with it. so it getting hided behind the segmented control.

SOLUTION

step 1. create uiviewcontroller (Not uitableviewcontroller)
step 2. add segmented control in the view
step 3. add static searchbar beneath the segmented control. step 4.so while scrolling the tableview,segmented control will not be hidden

Hope this will help you.

karthik
  • 621
  • 4
  • 14