0

I have a custom titleView for displaying Navigation title and sub-title. The title font is bigger than sub-title. Here's what I have now:

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;

I want to have the ability to remove subTitleView at some point in my code, but that makes the titleView look out of center (vertically).

So my question is, how would you implement auto-positioning here? should I use layoutSubviews ? if so, what would be the proper way to override it?

I thought that when the container view has an autoresizingMask it will adjust it's size according to the "total" size of it's subviews (?).

Thanks

4

1 回答 1

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];
}
于 2012-11-02T16:40:43.453 回答