1

I use a button as the titleView. But when I want to get the height of the button, whennever I use the self.button.frame.size.height or self.navigationItem.titleView.frame.size.height, it always return 0; Other value (like x, y and width) may seems reasonable.

Why?

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];
 }

And

-(void)doSomeThing:(id)sender
{
    NSLog(@"height%f", self.switchButton.frame.size.height);
}
NewXcoder
  • 705
  • 1
  • 5
  • 18

2 Answers2

0
self.navigationItems.navagationBar

doesn't seem to be correct.

What about

CGRectGetHeight(self.navigationItem.titleView.frame)
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Sorry, I just made a mistake. I mean when I use self.navigationItem.titleView.frame, otherthing was Ok, but the height always was zero – NewXcoder Dec 05 '12 at 00:59
0

You can use

First assign A view to TitleView

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

Then to find the height of titleView

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

and please keep this in mind that if any view is not assigned to titleView of the NavigationItem It will return you 0.000;

spider1983
  • 1,098
  • 1
  • 7
  • 14
  • that's returning the height of the whole bar, instead of the one of just the titleView – Gabriele Petronella Dec 04 '12 at 08:38
  • I see, I couple of observations. `autorelease` cannot be used with ARC apps (it's the default since iOS5 so I suppose he's using it). Moreover he's using a UIButton not a UILabel. Even if it probably does not change that much, it would be better to be precise. – Gabriele Petronella Dec 04 '12 at 08:50
  • i guess i won't post any code to use it as copy paste, i in-fact would like to show the way and let the fellow learn something. – spider1983 Dec 04 '12 at 09:14
  • But I have a question. If I don't set the frame manually, the navagation view will automatic layout the titleView. But when things like that, the height seems be zero, but I can see it in the navigation bar. – NewXcoder Dec 05 '12 at 01:14
  • basically titleView doesnot have its own frame it will give you the frame size of the view assigned to it, check in your code that the view is being set to titleview or not...if you post the part of the code where you are setting your tittle view,it will be more clear to us. – spider1983 Dec 05 '12 at 05:17
  • You can see the code now. If I don't set the frame manually, it always print out 0. – NewXcoder Dec 05 '12 at 05:35
  • I just told you in my previous comment that titleview doesnot have its own frame its just a container and whatever the frame of the view assigned to it is the frame you will get of titleview , if you don't set the frame of the view it will give default 0 so in any case you will have to use ** self.switchButton.frame = CGRectMake(0, 0, width, height); ** – spider1983 Dec 05 '12 at 05:46