1

I have solved an issue where a SegmentControl wasn't scrolling with my table view. I did this by embedding the control in my table view, like so:

UIView *headerView = [[UIView alloc] init ];
        [headerView addSubview:resultsSegment];
        self.tableView.tableHeaderView = headerView;

This works nicely......

but now I can't click on the segment control. Now it's embedded does that mean it's essentially BEHIND the TableView as far as a users touch is concerned?

Any ideas on how to make the SegmentControl "clickable" again?

Thanks

old_timer
  • 69,149
  • 8
  • 89
  • 168
Dan
  • 2,304
  • 6
  • 42
  • 69

2 Answers2

1

The segment control is NOT behind the tableView. It is ON the tableView.

To make the segmentControl clickable again, you need to set all the segmentControl properties. Here is some example code from the web.

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[segmentedControl addTarget:self
                     action:@selector(pickOne:)
           forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
Ed Chin
  • 1,214
  • 11
  • 24
  • I have used attributes inspector to set all of these parameters. The segment works, it just doesn't stay where it is when scrolled. Adding your code doesn't make a difference as it stands. – Dan Feb 17 '13 at 01:44
  • Okay, looks like you worked it out. To clarify, you needed to keep the control above tableView so that it stays in place while scrolling. Adding the control to the tableView header makes the control scroll with the tableView. Good luck. – Ed Chin Feb 17 '13 at 02:45
  • I now have this issue though, very strange: http://stackoverflow.com/questions/14918055/scrolling-tableview-effects-response-time-of-segmentcontrol – Dan Feb 17 '13 at 04:43
1

Thanks to @eddieios I did sort the problem out....by removing most of the code I quoted in my original answer.

As I use the storyboard and attributes inspector to set most of the parameters I found this to be the code that worked:

   resultsSegment.frame = CGRectMake(50, 100, 250, 40);
   [self.tableView addSubview:resultsSegment];

The first line simply made the segmentcontroller bigger in size and the second line not only allows me to scroll AND keep the segment controller in the same position but also USE it!

Dan
  • 2,304
  • 6
  • 42
  • 69