如何设置NavigationBar的barButtonItems

时间:2015-09-15 07:07:53

标签: ios uinavigationbar

现在,我发现问题可能来自我的自定义按钮--DJExchangeImageTitleButton。这是代码:

- (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
frame.size.width += DJMargin;
[super setFrame:frame];
}

- (void)layoutSubviews
{
 [super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可

// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;

// 2.计算imageView的frame
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + DJMargin;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];

// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
 {
    [super setImage:image forState:state];

// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
 -(void)setHighlighted:(BOOL)highlighted{};

谁能告诉我我的定制按钮有什么问题?我会非常感激。

当我设置navigationBar的left,right和titleView时,它是正常的; 但是,每次,我都推送到另一个控制器,然后弹出,左右barButtonItems变得越来越长,titleView越来越短。

这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];

[self setupNaviBar];}

-(void)setupNaviBar{

//leftButton
DJExchangeImageTitleButton *titleBtn  = [[DJExchangeImageTitleButton alloc]init];
[titleBtn setImage:[UIImage imageNamed:@"homgpage_navi_left"] forState:UIControlStateNormal];
[titleBtn setTitle:@"深圳" forState:UIControlStateNormal];
titleBtn.backgroundColor = [UIColor redColor];
titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
titleBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:titleBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                   target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftItem];


//searchBar
UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(0, 0, 250, 25);
btn.layer.cornerRadius = 3;
[btn setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
[btn setTitle:@"请输入要搜索的职位名称" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.imageEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
[btn addTarget:self action:@selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = btn;

//rightButton
DJExchangeImageTitleButton *notiBtn  = [[DJExchangeImageTitleButton alloc]init];

notiBtn.backgroundColor = [UIColor redColor];
[notiBtn setTitle:@"通知" forState:UIControlStateNormal];
[notiBtn setImage:[UIImage imageNamed:@"homgpage_navi_right"] forState:UIControlStateNormal];
notiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
notiBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:notiBtn];
self.navigationItem.rightBarButtonItem = right;

}

如何解决此问题?抱歉,我无法发布图片来显示我的问题。

0 个答案:

没有答案