4

我有一个使用以下代码行UIView添加到的子类:titleViewnavigationItem

self.navigationItem.titleView = tempview;

很容易。这很好用。我的问题是这navigationItem有时会rightBarButton更新(有时没有按钮,有时有一个标准大小的按钮,有时有一个更大的按钮)。

我想我可以简单地使用我添加layoutSubviews的类的方法,所以我把它放进去:tempviewtitleView

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

这似乎不起作用,因为在rightBarButton更新项目时它没有正确调整标题视图的大小。

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

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

我还确保该rightBarButton项目设置为 nil,但是一旦缩小,视图仍然无法正确展开。

谢谢你的帮助!

4

3 回答 3

4

配置您的视图

[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

(可能在 initWithFrame 中)

然后实施

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

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

于 2011-04-26T17:35:02.593 回答
2

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.

于 2010-02-04T03:22:25.943 回答
0

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

tempView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
于 2010-02-04T00:56:37.920 回答