自动布局与UINavigationBar的和的UIBarButtonItem [英] Auto Layout with UINavigationBar and UIBarButtonItem
问题描述
我喜欢创造自己的看法作为独立的厦门国际银行文件,然后实例化并添加为子视图。
I like to create my views as standalone Xib files then instantiate them and add as subviews.
所以,用时一个 UINavigationBar的
,我希望能够做同样的事情,首先要创建我的自定义视图 - 从厦门国际银行 - 然后将其添加为自定义视图的的UIBarButtonItem
:
So, when working with a UINavigationBar
, 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 UIBarButtonItem
:
UIBarButtonItem *anItem = [[UIBarButtonItem alloc] initWithCustomView:_myCustomView];
然后增加了导航栏:
Then adding to the nav bar:
self.navigationBar.topItem.rightBarButtonItems = @[ anItem, anotherItem ];
到目前为止好。
So far so good.
现在, _myCustomView
使用自动布局(AL),我认为这将是没有问题的。的不是这样的。的我试过,一应俱全。没有奏效。我甚至尝试添加自定义视图具有导航栏控制器的一个子视图。认为作为视图层次兄弟姐妹,AL将会把它当作外常规视图的 UINavigationBar的
。
Now, _myCustomView
uses Auto Layout (AL) and I thought this would be no problem. Not the case. 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 UINavigationBar
.
这也不能工作。控制器的 updateViewConstraints
叫,但从来没有使用。视图的初始帧住在 CGRectZero
。这是因为如果AL看到该视图是在顶部的 UINavigationBar的
,即使是兄弟姐妹,并决定它不需要铺设了。
That didn't work either. The controller's updateViewConstraints
was called but never applied. The view's initial frame stayed at CGRectZero
. It's as if AL sees that the view is on top of a UINavigationBar
, even as a sibling, and decides it doesn't need laying out.
当然,我试过 bringSubviewToFront
, translatesAutoresizingMaskIntoConstraintstranslatesAutoresizingMaskIntoConstraints
,等等。后者给了可爱的:
Of course, I've tried bringSubviewToFront
, translatesAutoresizingMaskIntoConstraintstranslatesAutoresizingMaskIntoConstraints
, and so on. The latter gave the lovely:
终止应用程序由于未捕获的异常
NSInternalInconsistencyException,理由是:自动布局仍
执行-layoutSubviews后需要。 MyNavigationBar的
实施-layoutSubviews的需要调用超。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MyNavigationBar's implementation of -layoutSubviews needs to call super.'
所以,问题是,有没有人从厦门国际银行加载自定义视图与AL,并成功地将此作为 customView
在的UIBarButtonItem
?如果是这样,怎么样?
So, the question is, has anyone loaded a custom view with AL from a Xib, and successfully set this as a customView
on a UIBarButtonItem
? If so, how?
推荐答案
看起来像的副本iOS的自动布局和UIToolbar / UIBarButtonItems
UIBarButtonItems
不会从的UIView
继承,因此,使用它们内部基于自动布局自定义视图可能无法完全支持的。听起来像是你需要做的pre自动布局的事情,直到问题得到解决。我建议提交的bug。
UIBarButtonItems
do not inherit from UIView
, so using auto-layout based custom views within them might not be fully supported. Sounds like you have to do the pre auto layout thing until the issue is resolved. I would suggest filing a bug.
编辑:由pre自动布局的事情,我的意思是直接操纵你的视图帧
By the pre auto layout thing, I mean manipulating the frame of your views directly.
这篇关于自动布局与UINavigationBar的和的UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!