Swift SearchBar如何在以编程方式更改后获取原始搜索图标

时间:2017-01-23 23:57:52

标签: ios swift

我目前正在使用searchBar我将左侧图标更改为自定义UIImageView。当按下按钮时,我想将其更改回原始搜索图标。

//my custom search iconBar 
searchBar.setImage(UIImage(named:"img1"), for: .search, state: .normal)

func changeSearchBarIconBackToOriginal {
//searchbar icon should be back to original image here
}

1 个答案:

答案 0 :(得分:0)

您可以将最后一张图像存储在变量中,并将搜索栏的图标设置为按下该按钮时的图标。

示例:

var lastIcon: UIImage?

...

lastIcon = searchBar.image(for: .search, state: .normal)
searchBar.setImage(UIImage("img"), for: .search, state: .normal)

然后,在按下的功能中:     func changeSearchBarIconBackToOriginal(){         searchBar.setImage(lastIcon,for:。search,state:.normal)     }