-1

I was set pdf vector image in UINavigationBar rightBarButtonItem but it still displaying too small.

enter image description here

How to display full size vector image in UINavigationBar?

I was try following code:-
First way:-

UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@“imgRightArrow”] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = barbutton;

Second way:-

UIImage *image = [UIImage imageNamed:@“imgRightArrow”]
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button.imageView setContentMode:UIViewContentModeScaleToFill];
button.frame = CGRectMake(0, 0, 35, 35);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Bhaumik Surani
  • 1,730
  • 3
  • 19
  • 40

2 Answers2

1

Problem is that the vector image has size declared in pixels. This does matter for some reason for tabBarItems and for UIBarButtonItem. I solved the problem with this UIImage extension.

func resize(to size: CGFloat) -> UIImage {
    let newSize = CGSize(width: size, height: size)
    return resize(newSize: newSize)
}


private func resize(newSize: CGSize) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    self.draw(in: CGRect(origin: CGPoint.zero, size: newSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage ?? UIImage()
}

 private lazy var sendAction = UIBarButtonItem(image: yourImage.resize(to: 32),
                                              style: .done,
                                              target: self,
                                              action: #selector(someAction))
0

What I do is to save the icon three times, 25px, 50px, 75px, and add this three together in assets ( x1, x2, x3), with this you dont have to set the size of the icon in your code.