在开发过程中,很多时候给导航栏设置一个title不能满足项目的需求,有时候我们还要给这个title添加相应的图片,具体做法如下。
//创建一个Base视图
UIView *titleBaseView = [[UIView alloc] initWithFrame:CGRectMake((WIDTH_VIEW - 70)/2, 11, 70, 22)];
//titleBaseView.backgroundColor = [UIColor redColor];
//创建一个ImageView
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"倒计时"]];
//创建一个Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 0, 70 - 22, 22)];
titleLabel.font = [UIFont systemFontOfSize:18.0];
titleLabel.textColor = TITLE_COLOR_DEEP;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"时间";
//将两个控件添加到Base视图上
[titleBaseView addSubview:titleImageView];
[titleImageView addSubview:titleLabel];
//将Base视图设置为导航栏标题视图
self.navigationItem.titleView = titleBaseView;
复制代码