2012-10-30 5 views
0

У меня есть пользовательский заголовокView для отображения названия навигации и подзаголовка. Титульный шрифт больше, чем подзаголовок. Вот что я сейчас:Автоматическое позиционирование позиции UINavigationBar's 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. Заголовок подписи всегда прикреплен. Subview подзаголовка субтитров может быть удалена/добавлена ​​всякий раз, когда это необходимо - то, что вызовет 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]; 
} 
Смежные вопросы