标签:
自定义标题栏按钮 @implementation SNTitleButton - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //图片居中 self.imageView.contentMode = UIViewContentModeCenter; //字体居右 self.titleLabel.textAlignment = NSTextAlignmentRight; //字体 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; self.titleLabel.font = [UIFont systemFontOfSize:20]; //高亮状态下图片不变色 self.adjustsImageWhenHighlighted = NO; } return self; } //设置图标的frame -(CGRect)imageRectForContentRect:(CGRect)contentRect { CGFloat imageY = 0; CGFloat imageW = self.height; CGFloat imageH = self.height; CGFloat imageX = self.width - self.height; return CGRectMake(imageX, imageY, imageW, imageH); } //设置标题的frame -(CGRect)titleRectForContentRect:(CGRect)contentRect { CGFloat imageX = 0; CGFloat imageY = 0; CGFloat imageW = self.width - self.height; CGFloat imageH = self.height; return CGRectMake(imageX, imageY, imageW, imageH); } @end 在控制器中调用自定义导航栏标题按钮 SNTitleButton *titleButton = [[SNTitleButton alloc]init]; //文字 [titleButton setTitle:@"首页" forState:UIControlStateNormal]; [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //图片 [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted]; //尺寸 titleButton.width = 100; titleButton.height = 35; self.navigationItem.titleView = titleButton;
标签:
原文地址:http://www.cnblogs.com/jsnan/p/4307789.html