码迷,mamicode.com
首页 > 其他好文 > 详细

自定义导航栏标题按钮

时间:2015-03-01 23:47:19      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

自定义标题栏按钮
@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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!