1

我使用一个按钮作为titleView。但是当我想获得按钮的高度时,当我从不使用self.button.frame.size.heightorself.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);
}
4

2 回答 2

0
self.navigationItems.navagationBar

似乎不正确。

关于什么

CGRectGetHeight(self.navigationItem.titleView.frame)
于 2012-12-04T08:36:16.457 回答
0

您可以使用

首先将A视图分配给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;

于 2012-12-04T08:36:24.317 回答