0

I have an XIB View that has a child view (red square) that I've vertically and horizontally centered using constraints.

Looks like below:

enter image description here

I am programatically adding the view to a view controller with the following:

xibView.frame = self.view.bounds;
self.view addSubview:beerMenuAddView];

However, the xibView and child view (red square) is no longer vertically centered. It appears as though the view is not refreshing/re-sizing to take into account the navbar.

I'm sure this is a simple fix... but I'm not sure what needs to happen here.

enter image description here

1 Answer 1

1

The navbar overlays the views below it. This behavior was changed in iOS 7. To visually center the red square, you have a couple options.

  1. Set the xibView.frame (height and x) values to take account of the space used by the navbar (and status bar if you're showing that too).
  2. Or, likely a better option, design the xibView in Interface Builder with the navbar showing and adjust the constraints to give you the desired centering.
5
  • Hi picciano, thanks for the thoughts. #2 sounds like a plan. However, I'm trying to get this to work by using "Simulated Metrics" on the view and adding a fake "Top Bar" I've then repositioned my child view and reset constraints, however when I add the subview it still hasn't moved.
    – aherrick
    Feb 24, 2015 at 23:19
  • 1
    I have run into something similar. To get the constraints to work the way I wanted, I had to create two nested subviews. The outer view had constraints that defined the entire area below the navbar. The inside view had a fixed height and width, but set to center within its parent.
    – picciano
    Feb 25, 2015 at 0:08
  • I see. So the nested view had a height of 64 from the top then? Everything else was pinned to each side. Then a secondary child view which was vertically/horizontally centered within it?
    – aherrick
    Feb 25, 2015 at 2:03
  • Exactly. Not sure if there is a better way to do that, but I have used that technique before.
    – picciano
    Feb 25, 2015 at 2:23
  • picciano, how might this code look? I am not sure how the constraint would then work if the phone was rotated on it's side. Shouldn't I not have a hardcoded 64px value? Thanks for your help...
    – aherrick
    Feb 25, 2015 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.