【发布时间】:2019-08-19 02:40:42
【问题描述】:
我在 UIViewController 扩展的函数内设置了一个 Button。我将这两个图像设置为两个不同的状态。但这在按下按钮时不会改变。我该如何解决这个问题?
我尝试了这里提供的所有解决方案。
import Foundation
import UIKit
extension UIViewController {
func addSortAndWeatherButton(sortAction: Selector, weatherAction: Selector){
let sortButton: UIButton = UIButton(type: UIButton.ButtonType.custom)
sortButton.setImage(UIImage(named: "sort-near.png"), for: .normal)
sortButton.setImage(UIImage(named: "sort-rating.png"), for: .selected)
sortButton.addTarget(self, action: sortAction, for: .touchUpInside)
sortButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
let sortBarButton = UIBarButtonItem(customView: sortButton)
self.navigationItem.rightBarButtonItems = sortBarButton
}
【问题讨论】: