为什么UIBarButtonItem图片总是模糊不清的像素化?

问题描述 投票:3回答:3

这是我现在的代码。

var reply = UIBarButtonItem(image: UIImage(named: "reply"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("reply:"))
self.navigationItem.rightBarButtonItem = reply

img右上角的按钮总是模糊不清。这是iPhone4s设备的截图,所以这不是一个与视网膜相关的问题。

我已经尝试了不同的图片尺寸,从30x30到512x512,并使用customView添加图片。这些方法都没有解决这个问题。

先谢谢你。

xcode swift uibutton uibarbuttonitem uinavigationitem
3个回答
10
投票

我使用这个方法解决了这个问题。

var replyBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
replyBtn.setImage(UIImage(named: "reply"), forState: UIControlState.Normal)
replyBtn.addTarget(self.navigationController, action: Selector("reply:"), forControlEvents:  UIControlEvents.TouchUpInside)
var item = UIBarButtonItem(customView: replyBtn)
self.navigationItem.rightBarButtonItem = item

它使用完全相同的图像显示一个非常清晰的按钮。


3
投票

从IOS人机界面指南来看,图标应该是22x22,请看这里的文档。https:/developer.apple.comlibraryiosdocumentationUserExperienceConceptualMobileHIGBarIcons.html。


0
投票

试试这个

func createBarButton(image: String, size: CGSize, offset: (x: CGFloat, y: CGFloat) = (0,0), hightlightable: Bool = true, action: Selector) -> UIBarButtonItem {
    let btn = UIButton(type: .custom)
    let img = UIImage(named: image)

    btn.setBackgroundImage(img, for: .normal)
    btn.addTarget(self, action: action, for: .touchUpInside)
    btn.frame = CGRect(x: offset.x, y: offset.y, width: size.width, height: size.height)
    btn.adjustsImageWhenHighlighted = hightlightable
    let view = UIView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
//        view.bounds = view.bounds.offsetBy(dx: offset.x, dy: offset.y)
    view.addSubview(btn)
    let barButton = UIBarButtonItem(customView: view)

    return barButton
}

self.navigationItem.rightBarButtonItem = createBarButton(image: "YOUR_IMAGE",
     size: CGSize(width: 35, height: 35),
     offset: (x: -10, y: 0),
     action: #selector(showXY))
© www.soinside.com 2019 - 2024. All rights reserved.