UICollectionView with custom layout - how/where to add a supplementary view programmatically?
The idea is that you return UICollectionViewLayoutAttributes
for the supplementary views in layoutAttributesForElements(in:)
. For example, in your UICollectionViewLayout
subclass:
var headerAttributes: UICollectionViewLayoutAttributes!
override func prepare() {
headerAttributes = UICollectionViewLayoutAttributes(
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
with: IndexPath(item: 0, section: 0))
headerAttributes.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
if headerAttributes.frame.intersects(rect) {
return [headerAttributes]
} else {
return nil
}
}
The important part is that the attributes are created using the forSupplementaryViewOfKind:
initialiser. By returning attributes for supplementary views from this method the collection view knows that these views exist and where they are positioned and will call collectionView(_:viewForSupplementaryElementOfKind:at:)
to get the actual view from its data source.
Alex Stone
When people asked me what I wanted to do for work 10 years ago, I did not know too well, so I just said "Something to do with computers". As I look at the last 10 years, I see that I did quite a lot of work all kinds of computers. From fiddling with microcontrollers and assembler code to writing data processing scripts to physically assembling computer consoles. The big step forward came when I learned to think about software in a structured, object-oriented way, as this allowed me to make software do things that I want it to do. Right now I'm proficient in two high level programming languages - Objective-C and Java and have touched just about every framework available for iOS. I've also became a hacker/maker and have completed a number of projects involving software and hardware. Right now I'm in my early 30s and when I ask myself "What would I like to do in the next 10 years?", my answer is "something with the human brain". The seeds are already there - I've picked up an interest in biology, cognitive science and neuroscience, enough to converse with real people. I've done first-hand research into sleep and made discoveries. I've taken classes in synthetic biology, performing manipulations on the bacteria genome. I've learned a lot about the neurotransmitter systems of the human brain, as well as how a biological organism develops. It seems like there are a lot of similarities between the object-oriented concepts I use in the daily programming tasks and how biological organisms operate. This makes me hopeful that by the time I'm in my late 30s, I would be able to work and program some form of biological computer or just plain hack the human brain.
Updated on June 04, 2022Comments
-
Alex Stone almost 2 years
I'm working with an example of Circle Layout, using UICollectionView with a custom layout class defined in the storyboard.
Is there some sort of a tutorial or a step by step explanation of how to add a supplementary view over to a collection view that uses a custom layout?
I've been looking at the Introducing Collection Views example, but cannot really understand how supplementary views are defined in that project. It appears that they are registered in a storyboard using flow layout, but I'm at a loss as to how subsequent layout changes in that project move supplementary views around.