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

标签 iphone uiview uinavigationbar uinavigationitem

我正在尝试添加自定义控件作为 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/

相关文章:

iphone - 如何使用 iphone 计算我们汽车的速度

iphone - 无法将手势识别器应用于导航栏

iphone - 打开 native 应用程序。来自 Safari

iphone - 处理 UIImageView 上的触摸事件

ios - 使用 MonoTouch.CoreText 绘制多行文本

ios - 自定义UIView大小位置和IBaction

ios - 带有大标题的导航栏黑线

ios - UIView 图像中的缩放效果

ios - 更改一个 View Controller 的 UINavigationBar 颜色

ios - UIToolBar 位置到 UINavigationController 的顶部