我使用一个按钮作为titleView。但是当我想要获得按钮的高度时,无论何时使用self.button.frame.size.height
或self.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);
}
答案 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;