获取 navigationItem.titleView layoutSubviews 的正确边界
我有一个 UIView
的子类,我使用以下代码行将其添加为 navigationItem
的 titleView
:
self.navigationItem.titleView = tempview;
足够简单。效果很好。我的问题是这个 navigationItem
有时会更新 rightBarButton
(有时没有按钮,有时有一个标准尺寸的按钮,有时有一个更大的按钮)。
我想我可以简单地使用我添加为 titleView
的 tempview
类的 layoutSubviews
方法,所以我将其放入
-(void)layoutSubviews {
[super layoutSubviews];
self.mylabel.frame = self.bounds;
}
:似乎不起作用,因为当更新 rightBarButton
项目时,它无法正确调整标题视图的大小。
我还注意到,一旦边界变小,它们就不会增长,它们只是改变位置。
我尝试过使用 setNeedsLayout
和 layoutIfNeeded
但它们只是用不正确的边界“调整”视图的大小。
我还确保将 rightBarButton
项设置为 nil,但视图在缩小后仍然无法正确扩展。
感谢您的帮助!
I have a subclass of UIView
that I've added to as the titleView
of a navigationItem
using the following line of code:
self.navigationItem.titleView = tempview;
Easy enough. That works fine. My problem is that this navigationItem
sometimes has the rightBarButton
updated (sometimes there is no button, sometimes there is one standard sized button, sometimes there is a larger button).
I figured that I could simply use the layoutSubviews
method of the tempview
class that I've added as the titleView
so I put this in:
-(void)layoutSubviews {
[super layoutSubviews];
self.mylabel.frame = self.bounds;
}
This does not seem to work, as it does not correctly resize the titleview when the rightBarButton
item is updated.
I've noticed also that the bounds do not grow once they gotten smaller, they simply change the position.
I've tried using setNeedsLayout
and layoutIfNeeded
but those simply "resize" the view with the incorrect bounds.
I've also made sure that the rightBarButton
item is set to nil, but the view still does not correctly expand once shrunk.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
配置您的视图
(可能在 initWithFrame 中)
然后实现,
现在您的视图与容器的大小相匹配,并自动调整大小。
configure your view with
(probably in the initWithFrame)
then implement
now you have your view matching the size of the container, and resizing automatically.
默认情况下,layoutSubviews 不执行任何操作。您也永远不会更改 titleView 的大小,因此除非导航栏为您执行此操作,否则没有任何变化也就不足为奇了。
由于您只更改了子视图之一的大小,因此我认为 autoresize 不起作用,因为我很确定当超级视图(启用了 autoresizesubviews 的视图)更改大小时会触发它。我建议在更改右键时重新计算标题视图的大小。如果它在您第一次添加时自动适合,您可以删除并重新添加它,但我知道这是一个非常丑陋的黑客行为。
By default, layoutSubviews does nothing. You also never change the size of the titleView, so unless a navbar does that for you, it's no surprise that nothing is changing.
Since you're only changing the size of one of the subviews, I don't think autoresize will work, as I'm pretty sure it's triggered when the superview (the one with autoresizesubviews enabled) changes size. I would suggest recalculating the size of the titleview when you change the rightbutton. If it automatically fits when you add it the first time, you could remove and readd it, but I know that's a pretty ugly hack.
您是否尝试过设置 autoresizingMask 属性,而不是覆盖
-layoutSubviews
?Instead of overriding
-layoutSubviews
have you tried setting the autoresizingMask property?