简体   繁体   中英

iOS UINavigationBar button image is too small for pdf vector icon

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

在此处输入图片说明

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];

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))

我所做的是将图标保存三次,25px、50px、75px,并将这三个一起添加到资产(x1、x2、x3)中,这样您就不必在代码中设置图标的大小。

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