2012-10-30 52 views
0

我有一個自定義titleView來顯示導航標題和子標題。 標題字體大於子標題。 這是我現在有:UINavigationBar的自動位置子視圖titleView

CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44); 
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease]; 
_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor]; 
_headerTitleSubtitleView.autoresizesSubviews = YES; 

CGRect titleFrame = CGRectMake(0, 0, 200, 24); 
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease]; 
titleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
titleView.backgroundColor = [UIColor clearColor]; 
titleView.font = [UIFont boldSystemFontOfSize:20]; 
titleView.textAlignment = UITextAlignmentCenter; 
titleView.textColor = [UIColor whiteColor]; 
titleView.shadowColor = [UIColor darkGrayColor]; 
titleView.shadowOffset = CGSizeMake(0, -1); 
titleView.text = @"Title"; 
titleView.adjustsFontSizeToFitWidth = YES; 

CGRect subtitleFrame = CGRectMake(0, 24, 200, 20); 
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease]; 
subtitleView.backgroundColor = [UIColor clearColor]; 
subtitleView.font = [UIFont boldSystemFontOfSize:13]; 
subtitleView.textAlignment = UITextAlignmentCenter; 
subtitleView.textColor = [UIColor whiteColor]; 
subtitleView.shadowColor = [UIColor darkGrayColor]; 
subtitleView.shadowOffset = CGSizeMake(0, -1); 
subtitleView.text = @"subtitle"; 
subtitleView.adjustsFontSizeToFitWidth = YES; 


_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
              UIViewAutoresizingFlexibleRightMargin | 
              UIViewAutoresizingFlexibleTopMargin | 
              UIViewAutoresizingFlexibleBottomMargin); 



[_headerTitleSubtitleView addSubview:titleView]; 
[_headerTitleSubtitleView addSubview:subtitleView]; 

self.navigationItem.titleView = _headerTitleSubtitleView; 

我想有在我的代碼, 一些點上,拆下subTitleView的能力,但使titleview的看出來中心(垂直)的。

所以我的問題是,你會如何在這裏實現自動定位? 我應該使用layoutSubviews嗎? 如果是這樣,將覆蓋它的正確方法是什麼?

我認爲,當容器視圖有一個autoresizingMask時,它會根據它的子視圖(?)的「總」大小調整它的大小。

感謝

回答

0

我能夠通過創建一個單獨的視圖類來解決這個問題, 實現layoutSubviews。 標題標籤子視圖始終附加。 只要需要,可以刪除/添加字幕標籤子視圖 - 哪個 將調用layoutSubViews來重新放置標題標籤視圖的中心位置。

- (id)initWithFrame:(CGRect)frame 
{ 
    CGRect containerFrame = CGRectMake(0, 0, 200, 44); 
    self = [super initWithFrame:containerFrame]; 
    if (self) { 

     self.backgroundColor = [UIColor clearColor]; 
     self.autoresizesSubviews = YES; 

     _title = [[UILabel alloc] initWithFrame:CGRectZero]; 
     _title.backgroundColor = [UIColor clearColor]; 
     _title.font = [UIFont boldSystemFontOfSize:20]; 
     _title.textAlignment = UITextAlignmentCenter; 
     _title.textColor = [UIColor whiteColor]; 
     _title.shadowColor = [UIColor darkGrayColor]; 
     _title.shadowOffset = CGSizeMake(0, -1); 
     _title.adjustsFontSizeToFitWidth = YES; 

     _subTitle = [[UILabel alloc] initWithFrame:CGRectZero]; 
     _subTitle.backgroundColor = [UIColor clearColor]; 
     _subTitle.font = [UIFont boldSystemFontOfSize:13]; 
     _subTitle.textAlignment = UITextAlignmentCenter; 
     _subTitle.textColor = [UIColor whiteColor]; 
     _subTitle.shadowColor = [UIColor darkGrayColor]; 
     _subTitle.shadowOffset = CGSizeMake(0, -1); 
     _subTitle.adjustsFontSizeToFitWidth = YES; 

     self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
              UIViewAutoresizingFlexibleRightMargin | 
              UIViewAutoresizingFlexibleTopMargin | 
              UIViewAutoresizingFlexibleBottomMargin); 
     [self addSubview:_title]; 
    } 
    return self; 
} 


- (void)layoutSubviews { 
    [super layoutSubviews]; 
    if (([self.subviews count] > 1) && ([[self.subviews objectAtIndex:1] isKindOfClass:[UILabel class]])) 
    { 
      [[self.subviews objectAtIndex:0] setFrame:CGRectMake(0,0,200,24)]; 
      [[self.subviews objectAtIndex:1] setFrame:CGRectMake(0,24,200,20)]; 
    } 
    else 
     [[self.subviews objectAtIndex:0] setFrame:self.bounds]; 
} 
相關問題