Search code examples
iosuinavigationitemimage-scaling

Make navigation button bigger


I have a custom icon on my navigation bar and even when i set the size to 600 x 400 it still shows to small on the bar.

enter image description here My code for it is :

-(void)goBackOne {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backbtn.frame = CGRectMake(0, 0, 35, 25);
    [backbtn setBackgroundImage:[UIImage imageNamed:@"done_button.png"] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(goBackOne) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button
    self.navigationItem.hidesBackButton = YES;//hide original back button


}

Solution

  • If your frame is 35x25 (...frame = CGRectMake(0, 0, 35, 25)), it doesn't matter if your picture is 4096x2160.

    You have a few options.

    You could set the picture to the proper size and:

    UIImage buttonImage* = [UIImage imageNamed:@"done_button.png"]; // png size ~40x40 for retina
    backbtn.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    

    or manually set the frame with an obvious

    backbtn.frame = CGRectMake(0, 0, 40, 35); 
    

    and programmatically make your UIImage to scale or stretch inside the frame bounds. Stretch a UIImage.

    also you could try implementing a normal back button through storyboard/xibs and simply call setImage if you don't mind letting Objective-C do the heavy lifting. (see: How not to stretch an image for UIButton)