0

It's a really tough bug to squash, my ObjC-fu is just really beginner and my confidence is crashing the longer I look into this pretty bug...

The App is required to have a view (let's call it mainView) with UIPageControl and UIScrollView in order to have 2 views scrollable horizontally. Hence I have implemented it by adding:

// This is added on viewDidLoad...
[self addChildViewController:[self.storyBoard instantiateViewControllerWithIdentifier:@"VC1"]];
[self addChildViewController:[self.storyBoard instantiateViewControllerWithIdentifier:@"VC2"]];

// Further processing of the child view controllers...
self.scrollView.pagingEnabled = YES;

for (NSUInteger i = 0; i < [self.childViewControllers count]; i++) {
    UIViewController *controller = [self.childViewControllers objectAtIndex:page];
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    controller.view.frame = frame;
    [self.scrollView addSubview:controller.view];
}

// Expanding scroll view's content size for scrolling...
self.scrollView.contentSize = CGSizeMake(
    scrollView.frame.size.width * [self.childViewControllers count], 
    scrollView.frame.size.height);

Note that mainView is itself equipped with UISearchDisplayController having a UISearchBar hidden offscreen. (Not sure if this information is vital.)

The problem is in the second child view controller with identifier VC2 is also equipped with UISearchDisplayController having a UISearchBar, and when the search bar dismisses on end searching, the animation some how make the VC2's UISearchBar to reposition in CGRect(0, 0, 320, 40) of mainView instead of VC2. Hence the VC2's UISearchBar "warped" back into VC1.

First attempt to solve this is containing each child view controller in another view hoping the CGRect(0, 0, 320, 40) of VC2's UISearchBar will remain back in VC2, but to no avail:

for (NSUInteger i = 0; i < [self.childViewControllers count]; i++) {
    UIViewController *controller = [self.childViewControllers objectAtIndex:page];
    CGRect frame = self.scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    //controller.view.frame = frame;
    UIView *container = [[UIView alloc] initWithFrame:frame];
    [container addSubview:controller.view];
    [self.scrollView addSubview:container];
}

Somehow I am guessing when VC2's UISearchDisplayController animates it's UISearchBar the frame that it uses are actually the one of self.UIScrollView but I am not sure.

Could anyone please shed some light on this bug and burn it to death please? It's in iOS7

     Main View
-------------------
|      VC1        |       VC2
-------------------------------------
|                 |_________________| <- Search Bar (This will 'warped' to VC1 at the same
|                 |                 |    position on end editing)
|                 |                 |
|                 |                 |
|                 |                 |
|                 |                 |
|                 |                 |
|                 |                 |
|                 |                 |
|                 |                 |
-------------------------------------
J.L
  • 75
  • 2
  • 10

1 Answers1

0

Found a solution, weird, but it works, got the idea from SearchBar disappears from headerview in iOS 7 after noticing that the memory address of the UISearchBar's superview was changed on end search.

The following code snippet was added any of the delegated methods possible, namely viewWillAppear, searchDisplayControllerWillEndSearch:, searchDisplayControllerDidEndSearch::

if (self.searchDisplayController.searchBar.superview != self.searchDisplayController.searchContentsController.view) {
    [self.searchDisplayController.searchBar removeFromSuperview];
    [self.searchDisplayController.searchContentsController.view addSubview:self.searchDisplayController.searchBar];
}

Will accept this as an answer but still should there be anything wrong with this do share. Thank you very much.

Community
  • 1
  • 1
J.L
  • 75
  • 2
  • 10