自定义iOS6中的导航栏按钮

时间:2013-12-02 08:54:35

标签: ios uinavigationbar customization

更新 - 一种可能的解决方案

根据Alexander Merchi在这篇文章(UIBarButtonItem Custom view in UINavigationBar)中给出的答案,找到了解决方案。但仍然不知道如何正确改变图像的位置。

UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
        [btn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
        btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];

enter image description here

原帖

在iOS 6中,目标是这样的:

enter image description here

虽然目前的结果如下:

enter image description here

我的代码是

btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"]
                                           style:UIBarButtonItemStylePlain
                                          target:self 
                                          action:@selector(onClickMenuButton)];

button.png是白色的圆圈,里面有白色的三个条纹和一个透明的背景。

2 个答案:

答案 0 :(得分:8)

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:@"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:@"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;

答案 1 :(得分:1)

我正在通过

完成它
 UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_back_ontouch_scr11.png"]];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
    [leftButton setImage:uView.image forState:UIControlStateHighlighted];
    [leftButton setImage:[UIImage imageNamed:@"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
    [leftButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

    self.navigationItem.leftBarButtonItem = aButton;
    self.navigationItem.hidesBackButton = YES;

这应该有效。