簡體   English   中英

如何獲取NavigationBar的titleView的高度?

[英]How to get the height of navigationBar's titleView?

我使用一個按鈕作為titleView。 但是,當我想獲取按鈕的高度時,無論何時都不使用self.button.frame.size.heightself.navigationItem.titleView.frame.size.height ,它總是返回0; 其他值(例如x,y和width)似乎是合理的。

為什么?

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);
}
self.navigationItems.navagationBar

似乎不正確。

關於什么

CGRectGetHeight(self.navigationItem.titleView.frame)

您可以使用

首先將一個視圖分配給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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM