如何获得navigationBar的titleView的高度?

时间:2012-12-04 08:24:38

标签: ios uinavigationcontroller height uinavigationbar titleview

我使用一个按钮作为titleView。但是当我想要获得按钮的高度时,无论何时使用self.button.frame.size.heightself.navigationItem.titleView.frame.size.height,它总是返回0;其他值(如x,y和宽度)似乎是合理的。

为什么?

In loadView
{
    self.switchButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [self.switchButton setTitle:@"TapMe" forState:UIControlStateNormal];
    self.navigationItem.titleView = self.switchButton;
    [self.switchButton addTarget:self action:@selector(doSomeThing:) forControlEvents:UIControlEventTouchUpInside];
 }

-(void)doSomeThing:(id)sender
{
    NSLog(@"height%f", self.switchButton.frame.size.height);
}

2 个答案:

答案 0 :(得分:0)

self.navigationItems.navagationBar

似乎不正确。

怎么样?
CGRectGetHeight(self.navigationItem.titleView.frame)

答案 1 :(得分:0)

您可以使用

首先将一个视图分配给TitleView

UILabel *titiLbl = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]autorelease];
titiLbl.text = @"Title Name";
self.navigationItem.titleView = titiLbl;

然后找到titleView的高度

CGRect navigationframe = self.navigationItem.titleView.frame;
NSLog(@"navigationframe Height=%f",navigationframe.size.height);

请记住,如果没有为NavigationItem的titleView分配任何视图,它将返回0.000;