1

I have created a custom UINavigationBar which is a tiny bit taller than Apples default navigation bar.

I can’t seem to find a way to move the UIBarButtonItem down to be directly centered between the two dashed lines.

Is there an easy way to do this? I’ve tried creating a custom button but had no success. Ideally I would just like to move the default back button down a couple of pixels.

enter image description here

Code used to create UINavigationBar, custom header image and UIBarButtonItem:

    //Create image for navigation background
UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"navbackground.png"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground
                                   forBarMetrics:UIBarMetricsDefault];

//Centered title image
UIImageView *headerImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"hometitle.png"]];
    [headerImage setFrame: CGRectMake(0, 0, 180, 49)];
self.navigationItem.titleView = headerImage;

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
[backBarButtonItem setTintColor: [UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]];
[backBarButtonItem setStyle: UIBarButtonItemStylePlain];
    self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];

Thanks in advance,

Adam H
  • 1,521
  • 2
  • 16
  • 19

2 Answers2

3

Create the UIBarButtonItem using a custom view. This custom view will be a UIView with the actual UIButton (as a subview) placed x pixels from the top (x=the number of pixels you want to move it down).

Paul de Lange
  • 10,613
  • 10
  • 41
  • 56
-1

sorry

UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];  
 [myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];   myButton1.showsTouchWhenHighlighted = YES;  
 myButton1.frame = CGRectMake(0.0, 3.0, 50,30);  
  [myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];    
 self.navigationItem.leftBarButtonItem = leftButton;
Rajat
  • 10,977
  • 3
  • 38
  • 55
ganesh manoj
  • 977
  • 10
  • 24
  • Get a warning - "'UIBarButtonItem' may not respond to 'setFrame:’” and it causes the program to crash. – Adam H Jun 13 '12 at 10:59
  • sorry UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; [myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; myButton1.showsTouchWhenHighlighted = YES; myButton1.frame = CGRectMake(0.0, 3.0, 50,30); [myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1]; self.navigationItem.leftBarButtonItem = leftButton; – ganesh manoj Jun 13 '12 at 11:13