gpt4 book ai didi

ios - 如何将背景图像添加到 UIBarButtonItem

转载 作者:行者123 更新时间:2023-11-28 16:15:17 24 4
gpt4 key购买 nike

我有以下代码,当点击 textField 时,它会在键盘顶部添加一个 UIToolbar。

如何向其中一个按钮添加背景图片?

我试过...

toolBar.items![1].setBackButtonBackgroundImage(clearButton, forState:.Normal, barMetrics:.Default) 

但它没有用,我收到错误...

Use of unresolved identifier 'imageName'

代码:

func addButtonsToKeyboard(){
let toolBar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
toolBar.barStyle = UIBarStyle.Default
toolBar.tintColor = UIColor.blueColor()
toolBar.barTintColor = UIColor.grayColor()
toolBar.items = [
UIBarButtonItem(title: "Button1", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(someFunction)),
UIBarButtonItem(title: "Button2", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(someFunction)),
UIBarButtonItem(title: "Button3", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(someFunction))]

toolBar.sizeToFit()

toolBar.items![1].setBackButtonBackgroundImage(myImage, forState:.Normal, barMetrics:.Default)

myTextField.inputAccessoryView = toolBar
}

顺便说一句 - 图片 myImage 位于 Assets.xcassets 中。

最佳答案

也许您可以尝试创建一个 UIImage 变量,然后将其传递进来以使其更加明确:

var myImage = UIImage(named: "myImage")
toolBar.items![1].setBackButtonBackgroundImage(myImage, forState:.Normal, barMetrics:.Default)

关于ios - 如何将背景图像添加到 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com