我正在尝试将模板图像添加到幻灯片菜单 Controller (基于此 SideMenuController component )。但我对色调有疑问。原始图像是深灰色的,我希望它们是白色的。我已经设置了 .alwaysTemplate
模式并以编程方式着色颜色,但我得到的不是必需的行为:
即,仅当我点击该菜单项时,色调颜色才会从灰色变为白色。你知道如何解决这个问题吗?
这是我的导航菜单 Controller 代码:
class NavigationMenuController: UITableViewController {
@IBOutlet weak var groupsIcon: UIImageView!
@IBOutlet weak var calendarIcon: UIImageView!
@IBOutlet weak var documentsIcon: UIImageView!
@IBOutlet weak var settingsIcon: UIImageView!
private let gradientTop = UIColor.fromHexadecimal(0xF3736F)
private let gradientBottom = UIColor.fromHexadecimal(0xEC92AE)
private let cellSelection = UIColor.fromHexadecimal(0xEC92AE)
private let menuIconTint = UIColor.fromHexadecimal(0xFFFFFF)
private let segues = [
"embedGroupsController",
"embedCalendarController",
"embedRulesController",
"embedSettingsController"
]
override func viewDidLoad() {
super.viewDidLoad()
let gradient = CAGradientLayer()
gradient.colors = [gradientTop.cgColor, gradientBottom.cgColor]
gradient.locations = [0.0, 1.0]
gradient.frame = tableView.bounds
let backgroundView = UIView(frame: tableView.bounds)
backgroundView.layer.insertSublayer(gradient, at: 0)
tableView.backgroundView = backgroundView
let imageViews: [UIImageView] = [groupsIcon, calendarIcon, documentsIcon, settingsIcon]
for imageView in imageViews {
guard let image = imageView.image else { fatalError("Image not found") }
let templateImage = image.withRenderingMode(.alwaysTemplate)
imageView.image = templateImage
imageView.tintColor = menuIconTint
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
let bgColorView = UIView()
bgColorView.backgroundColor = cellSelection
cell.selectionStyle = .default
cell.backgroundColor = .clear
cell.selectedBackgroundView = bgColorView
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let sideMenu = sideMenuController else {
fatalError("Side menu controller is not configured")
}
if indexPath.row >= segues.count {
fatalError("Unexpected row index: \(indexPath.row)")
}
sideMenu.performSegue(withIdentifier: segues[indexPath.row], sender: nil)
}
}
我也尝试通过 Storyboard 和 Assets 设置来做到这一点,但效果相同。我在 iOS 10 和 Swift 3 上运行这个应用程序。
最佳答案
我对单元格内的 UIImageView
色调也有同样的问题。
我通过将 ImageView 色调设置为默认值并改为设置单元格的内容 View 色调来修复它。
关于ios - 单击 UITableViewCell 上的 UIImageView 模板图像色调颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44541673/