我在UINavigationBar rightBarButtonItem中设置了pdf矢量图像,但是它仍然显示得太小。
如何在UINavigationBar中显示完整尺寸的矢量图像?
我尝试使用以下代码:-
第一种方式:-
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@“imgRightArrow”] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = barbutton;
第二种方式:-
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];
答案 0 :(得分:1)
问题是矢量图像的大小以像素为单位声明。对于tabBarItems和UIBarButtonItem,这确实很重要。我用这个UIImage扩展解决了这个问题。
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))
答案 1 :(得分:0)
我要做的是保存图标三遍,分别为25px,50px,75px,并将这三个添加到素材资源(x1,x2,x3)中,您不必在代码中设置图标的大小