How to change the color of view in collection view when it select and when the select the other view before view came to original color How to set it?
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
You can use the didSelectItemAtIndexPath method and the didDeselectItemAtIndexPath method from the UICollectionViewDelegate protocol.
UICollectionViewDelegate
is an Objective-C protocol, indicated by@objc
, that allows you to have optional methods likedidSelectItemAtIndexPath
anddidDeselectItemAtIndexPath
that you can choose to implement optionally in your delegate.When
UICollectionView
gets instantiated initially, it goes through all the available methods you’ve implemented in the instance ofUICollectionViewDelegate
with responds(to:), which takes a selector parameter and returns a bool, to see which methods from theUICollectionViewDelegate
protocol you’ve implemented without actually sending a message to them. As it detects the following methods, it remembers them and calls those methods whenever you select or deselect a cell from the collection view:Alternatively, you can use the collectionView(_:didHighlightItemAt:) method and the collectionView(_:didUnhighlightItemAt:) method. The following sample code is from here:
You can simply add two methods for changing colour of the view or cell in the collectionView:-
//When cell got selected
//When cell deselected
If you have view inside your cell as backgroundView then you can use same functions and just need to replace cell.backgroundView.backgroundColor in place of the cell.backgroundColor. Just remember "backgroundView" is the name of the view you have inside cell.
If you want more information about this then you can follow below attached links:-
1). https://www.tutorialfor.com/questions-296884.html
2). https://en.it1352.com/article/fccf358a040b4ddba872d7324602754c.html