【问题标题】:Auto position subviews of UINavigationBar's titleViewUINavigationBar 的 titleView 的自动定位子视图
【发布时间】:2012-10-30 00:11:55
【问题描述】:

我有一个自定义 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 时,它会根据其子视图的“总”大小(?)来调整它的大小。

谢谢

【问题讨论】:

    标签: ios uinavigationbar title layoutsubviews titleview


    【解决方案1】:

    我能够通过创建一个单独的视图类和 实现布局子视图。 标题标签子视图始终附加。 可以在需要时删除/添加字幕标签子视图 - 这 将调用 layoutSubViews 将 Title 标签视图重新定位在中心。

    - (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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多