Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 479
George Avatar asked Dec 02 '25 05:12

George


1 Answers

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;
like image 84
Dinesh Avatar answered Dec 03 '25 20:12

Dinesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!