我将如何创建一个在 iOS 中同时具有标题和图像的条形按钮项目,图像按原样显示而没有色调?
How would I create a bar button item that has both title and image in iOS, with the image showing as is without a tint?
我在 stackoverflow 上找到了在 iOS 中创建一个栏按钮项目的解决方案,该项目同时具有标题和图像,但图像具有蓝色色调而不是显示原样。我在 stackoverflow 上找到了解决方案,可以在条形按钮项目中按原样显示图像而不显示蓝色,但条形按钮项目不显示标题。
是否可以创建一个既有标题又有图像的条形按钮项目,图像按原样显示?
这是我的代码:
let image = UIImage(named: "iMessage Icon 40x30.png")
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
button.setTitle("Purchase iMessage Saved Messages", for: .normal)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(self.tap), for: .touchUpInside)
button.sizeToFit()
let customBarButtonItem = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = customBarButtonItem
此代码使用原样图像创建条形按钮项目,没有蓝色色调,但标题不显示。如果我更改第二行代码并将 UIButton object 初始化为类型系统,标题和图像都会显示,但图像有蓝色调,这是我不想要的。
正如你在问题中提到的,类型为系统,你可以同时看到图像和标题。
所以,你的系统类型的 UIButton,并从你的代码中更改下面的行
button.setImage(image, for: .normal)
给这个:
button.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
这将不允许在您的图像上应用色调并将使用原始图像。
我同意上面的回答,但在我的例子中,下面的代码工作正常。
button.setImage(UIImage(named: "ImageName.png").withRenderingMode(.automatic), for: UIControl.State.normal)
我在 stackoverflow 上找到了在 iOS 中创建一个栏按钮项目的解决方案,该项目同时具有标题和图像,但图像具有蓝色色调而不是显示原样。我在 stackoverflow 上找到了解决方案,可以在条形按钮项目中按原样显示图像而不显示蓝色,但条形按钮项目不显示标题。
是否可以创建一个既有标题又有图像的条形按钮项目,图像按原样显示?
这是我的代码:
let image = UIImage(named: "iMessage Icon 40x30.png")
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
button.setTitle("Purchase iMessage Saved Messages", for: .normal)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(self.tap), for: .touchUpInside)
button.sizeToFit()
let customBarButtonItem = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = customBarButtonItem
此代码使用原样图像创建条形按钮项目,没有蓝色色调,但标题不显示。如果我更改第二行代码并将 UIButton object 初始化为类型系统,标题和图像都会显示,但图像有蓝色调,这是我不想要的。
正如你在问题中提到的,类型为系统,你可以同时看到图像和标题。
所以,你的系统类型的 UIButton,并从你的代码中更改下面的行
button.setImage(image, for: .normal)
给这个:
button.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
这将不允许在您的图像上应用色调并将使用原始图像。
我同意上面的回答,但在我的例子中,下面的代码工作正常。
button.setImage(UIImage(named: "ImageName.png").withRenderingMode(.automatic), for: UIControl.State.normal)