I'm not sure how to apply custom UIImage to editButtonItem .
In my view controller, I configured the bar button item as editButton
.
func configureNavigationBar() {
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.sizeToFit()
navigationItem.title = "TEST"
navigationItem.rightBarButtonItem = editButtonItem
extendedLayoutIncludesOpaqueBars = true
}
It gives me a default edit/done edit button, but now I have UIImage for the edit button and want to display them instead of the default edit/done button.
I also have setEditing
function in my view controller to set my collection view cells to the edit mode.
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if (editing){
collectionView.isEditing = true
} else {
collectionView.isEditing = false
}
}
It works perfectly until I add a custom UIimage to the editButtonItem.
I tried adding My custom UIimage using the following code.
navigationItem.rightBarButtonItem = UIBarButtonItem(image: Images.edit, style: .plain, target: self, action: #selector(setEditing(_:animated:)))
But when I implement the code above, I cannot trigger the setEditing function; it didn't go in the editing mode and stuck with not-editing mode forever.
I also tried keeping the following line
navigationItem.rightBarButtonItem = editButtonItem
and tried overriding the editButtonItem to something like,
override var editButtonItem: UIBarButtonItem {
get {
var result = UIBarButtonItem()
if isEditing {
print("isEdit true")
result = UIBarButtonItem(customView: UIImageView(image: Images.edit))
} else {
print("isEditfalse")
result = UIBarButtonItem(customView: UIImageView(image: Images.editDone))
}
return result
}
}
but it also makes the view stuck in the non-editing mode and cannot enter the editing mode. (as for the overriding editButtonItem, I guess I'm doing something wrong(?))
Could anyone point me out how to use the custom UIImage for the editButtonItem?
Thanks to @lazarevzubov, I used setRightBarButton() and add the following code and now it works.
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
rightBarButtonImage = collectionView.isEditing ? Images.edit.withRenderingMode(.alwaysOriginal) : Images.editDone.withRenderingMode(.alwaysOriginal)
navigationItem.rightBarButtonItem?.image = rightBarButtonImage
collectionView.isEditing = !collectionView.isEditing
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.