如何获得全尺寸UINavigationBar titleView

con*_*are 19 iphone uinavigationbar uiview uinavigationitem

我试图在UINavigationBar中添加自定义控件作为titleView.当我这样做,尽管设置框架和通常假设全宽的属性,我得到这个: 替代文字

明亮的蓝色可以忽略,因为它是我隐藏自定义控件的地方.问题是酒吧两端的窄条导航栏.如何摆脱这些,以便我的自定义视图将延伸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;
Run Code Online (Sandbox Code Playgroud)

PS - 我知道我可以自己添加视图而不是附加到导航栏,我自己很容易定位它.我有理由需要它在导航栏上","这些原因在这里

ksm*_*ksm 28

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

您所要做的就是子类UIView和覆盖setFrame.

像这样:

    - (void)setFrame:(CGRect)frame {
        [super setFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
    }
Run Code Online (Sandbox Code Playgroud)

现在设置你的鬼鬼祟祟的新UIView作为,navigationItem.titleView并享受其新发现的阻力调整超级视图.

每次设置帧时都不必设置超级帧.您可以设置一次并完成.如果你想支持方向改变,你可能也可能一起破解.

  • 我想硬编码值是一个糟糕的方式做到这一点,我来到这个代码-setFrame:超SETFRAME:CGRectMake(0.f,frame.origin.y,self.superview.frame.size.width,框架.size.height)]; (6认同)

Vde*_*edT 24

设置视图的titleView的navigationItem永远不会做到这一点.相反,您可以将子视图添加到导航控制器的navigationBar:

UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];
Run Code Online (Sandbox Code Playgroud)

  • 这种方法不适用于导航.将堆栈中的viewcontroller推到这里就可以了.我可以在推拉之前将其移除,但这看起来非常黑客 (2认同)