gpt4 book ai didi

iphone - 如何获得全尺寸的 UINavigationBar titleView

转载 作者:行者123 更新时间:2023-12-03 18:20:05 26 4
gpt4 key购买 nike

我正在尝试添加自定义控件作为 UINavigationBar 中的 titleView。当我这样做时,尽管设置了通常采用全宽的框架和属性,但我得到了: alt text

亮蓝色可以忽略,因为它是我隐藏自定义控件的地方。问题是导航栏两端的窄条。我怎样才能摆脱这些,以便我的自定义 View 将拉伸(stretch) 100%?

CGRect frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.width, kDefaultBarHeight);
UANavBarControlView *control = [[[UANavBarControlView alloc] initWithFrame:frame] autorelease];
control.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = control;

PS - 我知道我可以自行添加 View ,而不是附加到导航栏,并且自己定位它会非常容易。我有需要将其放置在导航栏“上”的原因,这些原因是 here

最佳答案

刚刚遇到了同样的问题,经过一番思考后解决了。 titleView 的框架每次出现时都会由 navigationBar 设置。

您所要做的就是子类化 UIView 并覆盖 setFrame

像这样:

    - (void)setFrame:(CGRect)frame {
[super setFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
}

现在将你偷偷摸摸的新 UIView 设置为 navigationItem.titleView 并享受它新发现的对 super View 调整大小的阻力。

您不必每次设置框架时都设置 super 框架。您只需设置一次即可完成。如果您想支持方向更改,您也可以将其组合在一起。

关于iphone - 如何获得全尺寸的 UINavigationBar titleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3756395/

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