简体   繁体   中英

Positioning image of navigationBar backButton

I add an image to my navigation controller back button like this:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ITButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];

It works fine, therefore, my image is not aligned with the < back symbol. The image is a little above the symbol, I want both centered. I don't know why. I've tried to adjust this using:

[self.navigationItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(10, 0, 0, 0)];

but nothing changed.

there are many ways to fix the issue, I was facing the same issue then I have Solved it this way

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([self.navigationController.viewControllers objectAtIndex:0] != self)
    {
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
        [backButton setImage:[UIImage imageNamed:@"home.png"] forState:UIControlStateNormal];
        [backButton setShowsTouchWhenHighlighted:TRUE];
        [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
        UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.hidesBackButton = TRUE;
        self.navigationItem.leftBarButtonItem = barBackItem;

    }
}

-(void)popViewControllerWithAnimation
{
    [self.navigationController popViewControllerAnimated:YES];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM