获取 navigationItem.titleView layoutSubviews 的正确边界

发布于 2024-08-20 07:50:17 字数 812 浏览 6 评论 0 原文

我有一个 UIView 的子类,我使用以下代码行将其添加为 navigationItemtitleView

self.navigationItem.titleView = tempview;

足够简单。效果很好。我的问题是这个 navigationItem 有时会更新 rightBarButton (有时没有按钮,有时有一个标准尺寸的按钮,有时有一个更大的按钮)。

我想我可以简单地使用我添加为 titleViewtempview 类的 layoutSubviews 方法,所以我将其放入

-(void)layoutSubviews {
    [super layoutSubviews];
    self.mylabel.frame = self.bounds;
}

:似乎不起作用,因为当更新 rightBarButton 项目时,它无法正确调整标题视图的大小。

我还注意到,一旦边界变小,它们就不会增长,它们只是改变位置。

我尝试过使用 setNeedsLayoutlayoutIfNeeded 但它们只是用不正确的边界“调整”视图的大小。

我还确保将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丿*梦醉红颜 2024-08-27 07:50:17

配置您的视图

[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

(可能在 initWithFrame 中)

然后实现,

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [self setFrame:[newSuperview bounds]];
}

现在您的视图与容器的大小相匹配,并自动调整大小。

configure your view with

[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

(probably in the initWithFrame)

then implement

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [self setFrame:[newSuperview bounds]];
}

now you have your view matching the size of the container, and resizing automatically.

挽袖吟 2024-08-27 07:50:17

默认情况下,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.

何处潇湘 2024-08-27 07:50:17

您是否尝试过设置 autoresizingMask 属性,而不是覆盖 -layoutSubviews

tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Instead of overriding -layoutSubviews have you tried setting the autoresizingMask property?

tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文