문제

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

도움이 되었습니까?

해결책

I was able to resolve this issue by creating a separate view class and implement layoutSubviews. Title label subview is always attached. Subtitle label subview can be removed/added whenever need - which will call layoutSubViews to make re-position the Title label view in center.

- (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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top