George
George

Reputation: 3619

Customize navigation bar button in iOS6

Update - One Possible Solution

Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.

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

Original Post

In iOS 6, the goal is like this:

enter image description here

while current result is like this:

enter image description here

My code is

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

button.png is this the white circle with white three bars inside and a tranparent background.

Upvotes: 3

Views: 3153

Answers (2)

Dinesh
Dinesh

Reputation: 939

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;

Upvotes: 8

Mihir Mehta
Mihir Mehta

Reputation: 13833

I am accomplishing it by

 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;

This should work.

Upvotes: 1

Related Questions