Weird interface bug UIScrollView in UITabBarController. Reproducible by others?

I get a weird interface bug with my UIScrollView and I cant figure out how to solve it. I only wrote one line of code (shown below) and it is a blank project’s setup easily reproducible!

Setting:

  • Swift :Segmented control with a swipe gesture between views
  • Add segmented control to navigation bar and keep title with buttons
  • How to change UISegmentcontrol font and selected segment colour?
  • How to set UISegmentedControl Tint Color for individual segment
  • Change UISegmentedControl selected index or value programmatically
  • Custom UISwitch with image
  • I have a UIScrollView that contains a UISegmentedControl (since the segments of
    the control are loaded dynamically, it could exceed the width of the screen and the scrollView is supposed to scroll the segmentedControl horizontally, the height of the scrollview is the same as the UISegmentedControl’s).
    The ViewController that contains this is embedded in a tabBar (or navigation bar, which also shows the bug). The whole thing is using Auto-Layout.

    Bug:

    When I scroll the SegmentedControl some degree to the right and then switch the viewController by clicking the other tab on the tabBarController, the content-offset of the segmented control gets weirdly shifted when switching back to the initial viewcontroller. When I try to scroll to the leftmost part of the scrollview it won’t let me. When switching the tabs a couple of times, it gets fixed again and I can do this over.

    What I did (can you reproduce this?):

    1. Create a blank single-view ios project
    2. Embed the already given viewController in a tabbarcontroller.
    3. Put a scrollView on the upper portion of the view that fits the screen from left to right.
    4. Put a UISegmentedControl on the topleft corner of the scrollview and drag the scrollview to fit the segmented controls height height
    5. Change the Segmented control’s width a bit so xcode adds a width-constraint. in the segmented control’s width constraint change the width constraint’s relation to “greater than or equal”
    6. create an outlet to the segmented control
    7. in viewDidload add this code

      [self.segmentedControl insertSegmentWithTitle:@"A really long title so it you have to scroll to see it" atIndex: 0 animated: NO];
      
    8. Create a blank viewcontroller and add it as a second viewController for the tabbarController.

    This is how my storyboard looks like:

    Storyboard with setup

    Now run the project, scroll the segmented control to it’s right end as far as it goes. Switch the tab and switch back and please tell me how your scrollview now behaves – and WHY.

    My guess would be it has something to do with Auto Layout maybe? Can’t figure out what though.

    I tried fixing this by setting the scrollView’s contentSize in viewDidAppear or changing the content offset of the scrollView in viewDidAppear or changing frames, combination of those and what not….

    Extra question:

    Is it no longer neccessary to set the scrollViews contentSize property? Why does it scroll the content automatically?

    Solutions Collect From Internet About “Weird interface bug UIScrollView in UITabBarController. Reproducible by others?”

    After googeling I found the answer in another StackOverflow question.

    What you need to do is save the scrollview.contentOffset on viewWillDisappear,
    set it to CGPointZero on viewDidDisappear and set it back to the saved state on viewDidLayoutSubviews:

    -(void) viewWillDisappear: (BOOL) animated { 
     self.lastContentOffset = self.scrollView.contentOffset;
    [super viewWillDisappear: animated];
    }
    
    -(void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear: animated];
    self.scrollView.contentOffset = CGPointZero;
    }
    - (void)viewDidLayoutSubviews {
    [super viewDidlayoutSubviews];
    self.scrollView.contentOffset = self.lastContentOffset;
    }