hanumanDev
hanumanDev

Reputation: 6614

How to move a bar button item within a nav bar

I have a custom bar button item that I want to nudge down 5 pixels. How would I go about doing that witht the following code. I tried to set the "y" point but keep getting syntax errors.

     [self.navigationController setNavigationBarHidden:NO animated:YES];

     UIImage *image = [UIImage imageNamed:@"arrow_back.png"];
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

     [self.navigationController.navigationBar addSubview:imageView];

    [imageView release];

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //set the frame of the button to the size of the image (see note below)
     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

     //create a UIBarButtonItem with the button as a custom view
     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
     self.navigationItem.leftBarButtonItem = customBarItem;

    [customBarItem release];
}

thanks for any help

Upvotes: 1

Views: 3790

Answers (1)

orogers
orogers

Reputation: 577

A UIBarButton item doesn't inherit from UIView so doesn't have a "frame" property that you can adjust directly so you need to use a different method.

Try:

    [self.navigationItem.leftBarButtonItem setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault];

Upvotes: 3

Related Questions