0

I'm currently having a UIViewcontroller with a 3-segment UISegmentedControl that when clicked switches view controllers displayed. The navigation bar and tab bar of this view are translucent.

I initialize the view like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    self.segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"View 1",@"View 2",@"View 3",nil]];
    [self.segControl setTintColor:[[ConstantsSingleton sharedConstants] default_bg_Color]];
    [self.segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [self.segControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
    [self.segControl setSelectedSegmentIndex:0];
    self.navigationItem.titleView = self.segControl;

    //Setting up the first viewcontroller
    UIViewController *vc = [self viewControllerForSegmentIndex:self.segControl.selectedSegmentIndex];
    [self addChildViewController:vc];
    vc.view.frame = self.contentView.bounds;
    [self.contentView addSubview:vc.view];
    self.currentViewController = vc;
}

The contentView is a IB defined UIView with 0 leading and trailing on all sides (so basically it fills the parent view).

I switch viewcontrollers the following way:

-(void)segmentChanged:(UISegmentedControl *)sender {
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    [self addChildViewController:vc];
    [self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
        vc.view.frame = self.contentView.bounds;
        [self.currentViewController.view removeFromSuperview];
        [self.contentView addSubview:vc.view];
    } completion:^(BOOL finished) {
        [vc didMoveToParentViewController:self];
        [self.currentViewController removeFromParentViewController];
        self.currentViewController = vc;
    }];
    self.navigationItem.title = vc.title;
}

Now whenever I run this with a opaque navigation bar and tab bar this works fine, but whenever I try to use a translucent navigation bar and/or tab bar only the first view gets resized/its insets adjusted properly to not be behind the translucent navigation bar and/or tab bar. The second and third view will still appear behind them when they appear on screen. It doesn't matter which viewcontroller is set as the first viewcontroller, all lead to the same behaviour.

What could be causing this issue and is there a way to solve this without resolving to manual contentinset adjustments.

Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70

2 Answers2

2

I would suggest to enable the option Extend Edges in the property inspector of you're view.

jonas vermeulen
  • 1,235
  • 5
  • 23
  • 40
0

Here's a good stackoverflow post differentiating the various layout settings for iOS 7 and up:

Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Community
  • 1
  • 1
kevinl
  • 4,194
  • 6
  • 37
  • 55