Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto Layout with UINavigationBar and UIBarButtonItem
    primarykey
    data
    text
    <p>I like to create my views as standalone Xib files then instantiate them and add as subviews.</p> <p>So, when working with a <code>UINavigationBar</code>, I expected to be able to do the same thing, first creating my custom view - from the Xib - then adding it as a custom view to the <code>UIBarButtonItem</code>:</p> <pre><code>UIBarButtonItem *anItem = [[UIBarButtonItem alloc] initWithCustomView:_myCustomView]; </code></pre> <p>Then adding to the nav bar:</p> <pre><code>self.navigationBar.topItem.rightBarButtonItems = @[ anItem, anotherItem ]; </code></pre> <p>So far so good. </p> <p>Now, <code>_myCustomView</code> uses Auto Layout (AL) and I thought this would be no problem. <em>Not the case.</em> I've tried just about everything. Nothing has worked. I even tried adding the custom view as a subview of the controller that has the navigation bar. Thinking that as siblings in the view hierarchy, AL would treat it as a regular view outside the <code>UINavigationBar</code>.</p> <p>That didn't work either. The controller's <code>updateViewConstraints</code> was called but never applied. The view's initial frame stayed at <code>CGRectZero</code>. It's as if AL sees that the view is on top of a <code>UINavigationBar</code>, even as a sibling, and decides it doesn't need laying out.</p> <p>Of course, I've tried <code>bringSubviewToFront</code>, <code>translatesAutoresizingMaskIntoConstraintstranslatesAutoresizingMaskIntoConstraints</code>, and so on. The latter gave the lovely:</p> <blockquote> <p>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MyNavigationBar's implementation of -layoutSubviews needs to call super.'</p> </blockquote> <p>So, the question is, has anyone loaded a custom view with AL from a Xib, and successfully set this as a <code>customView</code> on a <code>UIBarButtonItem</code>? If so, how?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload