gpt4 book ai didi

uinavigationbar - 使用 UINavigationBar 和 UIBarButtonItem 自动布局

转载 作者:行者123 更新时间:2023-12-04 12:54:52 25 4
gpt4 key购买 nike

我喜欢将我的 View 创建为独立的 Xib 文件,然后实例化它们并添加为 subview 。

因此,当使用 UINavigationBar 时,我希望能够做同样的事情,首先创建我的自定义 View - 从 Xib - 然后将其作为自定义 View 添加到 UIBarButtonItem :

UIBarButtonItem *anItem = [[UIBarButtonItem alloc] initWithCustomView:_myCustomView];

然后添加到导航栏:
self.navigationBar.topItem.rightBarButtonItems = @[ anItem, anotherItem ];

到现在为止还挺好。

现在, _myCustomView使用自动布局(AL),我认为这没问题。不是这样。我已经尝试了几乎所有的东西。没有任何效果。我什至尝试将自定义 View 添加为具有导航栏的 Controller 的 subview 。考虑到作为 View 层次结构中的兄弟,AL 会将其视为 UINavigationBar 之外的常规 View 。 .

那也没有用。 Controller 的 updateViewConstraints被调用但从未应用。 View 的初始帧停留在 CGRectZero .就好像 AL 看到 View 位于 UINavigationBar 之上,即使作为 sibling ,并决定它不需要布局。

当然,我试过 bringSubviewToFront , translatesAutoresizingMaskIntoConstraintstranslatesAutoresizingMaskIntoConstraints , 等等。后者给了可爱的:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MyNavigationBar's implementation of -layoutSubviews needs to call super.'



所以,问题是,有没有人从 Xib 加载一个带有 AL 的自定义 View ,并成功地将其设置为 customViewUIBarButtonItem ?如果是这样,如何?

最佳答案

看起来像 iOS Autolayout and UIToolbar/UIBarButtonItems 的副本
UIBarButtonItems不继承自 UIView ,因此可能不完全支持在其中使用基于自动布局的自定义 View 。听起来你必须做预先自动布局的事情,直到问题得到解决。我建议提交一个错误。

编辑:通过预自动布局,我的意思是直接操作 View 的框架。

关于uinavigationbar - 使用 UINavigationBar 和 UIBarButtonItem 自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17729947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com