Has anyone implemented a decoration view for the iOS 6 UICollectionView? It's impossible to find any tutorial on implementing a decoration view on the web. Basically in my app I have multiple sections, and I just wanted to display a decoration view behind each section. This should be simple to implement but I'm having no luck. This is driving me nuts... Thanks.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
I got this working with a custom layout with the following:
Create a subclass of UICollectionReusableView and for example add an UIImageView to it:
Then in your controller in viewDidLoad register this subclass with the following code (replace code with your custom layout)
In your custom layout then implement the following methods (or similar):
There seems to be no documentation for it, but the following document got me on the right track: Collection View Programming Guide for iOS
UPDATE: It is probably better to subclass UICollectionReusableView for a decoration view instead of UICollectionViewCell
Here's how to do it in MonoTouch:
With an end result similar to:
In my case : I wanted to upgrade from UITableView to UICollectionView.
uitableview sections >>> supplementary views
uitableview headerView >>> decoration view
In my case I felt subclassing layout and do other stuff, it's "too much" for just simple "headerView" (decoration)
So my solution was just to create the headerview (not section) as first cell and section 1 as the first section ( section 0 was size of zero)
if you want to add decorationview (Header /Footer) in collectionview
please check this link in this Header & Footer both added as DecorationView
http://www.appcoda.com/ios-collection-view-tutorial/
Here's a collection view layout decoration view tutorial in Swift (this is Swift 3, Xcode 8 seed 6).
Decoration views are not a UICollectionView feature; they essentially belong to the UICollectionViewLayout. No UICollectionView methods (or delegate or data source methods) mention decoration views. The UICollectionView knows nothing about them; it simply does what it is told.
To supply any decoration views, you will need a UICollectionViewLayout subclass; this subclass is free to define its own properties and delegate protocol methods that customize how its decoration views are configured, but that's entirely up to you.
To illustrate, I'll subclass UICollectionViewFlowLayout to impose a title label at the top of the collection view's content rectangle. This is probably a silly use of a decoration view, but it illustrates the basic principles perfectly. For simplicity, I'll start by hard-coding the whole thing, giving the client no ability to customize any aspect of this view.
There are four steps to implementing a decoration view in a layout subclass:
Define a UICollectionReusableView subclass.
Register the UICollectionReusableView subclass with the layout (not the collection view), by calling
register(_:forDecorationViewOfKind:)
. The layout's initializer is a good place to do this.Implement
layoutAttributesForDecorationView(ofKind:at:)
to return layout attributes that position the UICollectionReusableView. To construct the layout attributes, callinit(forDecorationViewOfKind:with:)
and configure the attributes.Override
layoutAttributesForElements(in:)
so that the result oflayoutAttributesForDecorationView(ofKind:at:)
is included in the returned array.The last step is what causes the decoration view to appear in the collection view. When the collection view calls
layoutAttributesForElements(in:)
, it finds that the resulting array includes layout attributes for a decoration view of a specified kind. The collection view knows nothing about decoration views, so it comes back to the layout, asking for an actual instance of this kind of decoration view. You've registered this kind of decoration view to correspond to your UICollectionReusableView subclass, so your UICollectionReusableView subclass is instantiated and that instance is returned, and the collection view positions it in accordance with the layout attributes.So let's follow the steps. Define the UICollectionReusableView subclass:
Now we turn to our UICollectionViewLayout subclass, which I'll call MyFlowLayout. We register MyTitleView in the layout's initializer; I've also defined some private properties that I'll need for the remaining steps:
Implement
layoutAttributesForDecorationView(ofKind:at:)
:Override
layoutAttributesForElements(in:)
; the index path here is arbitrary (I ignored it in the preceding code):This works! A title label reading ``Testing'' appears at the top of the collection view.
Now I'll show how to make the label customizable. Instead of the title "Testing," we'll allow the client to set a property that determines the title. I'll give my layout subclass a public
title
property:Whoever uses this layout should set this property. For example, suppose this collection view is displaying the 50 U.S. states:
We now come to a curious puzzle. Our layout has a
title
property, the value of which needs to be communicated somehow to our MyTitleView instance. But when can that possibly happen? We are not in charge of instantiating MyTitleView; it happens automatically, when the collection view asks for the instance behind the scenes. There is no moment when the MyFlowLayout instance and the MyTitleView instance meet.The solution is to use the layout attributes as a messenger. MyFlowLayout never meets MyTitleView, but it does create the layout attributes object that gets passed to the collection view to configure MyFlowLayout. So the layout attributes object is like an envelope. By subclassing UICollectionViewLayoutAttributes, we can include in that envelope any information we like — such as a title:
There's our envelope! Now we rewrite our implementation of
layoutAttributesForDecorationView
. When we instantiate the layout attributes object, we instantiate our subclass and set itstitle
property:Finally, in MyTitleView, we implement the
apply(_:)
method. This will be called when the collection view configures the decoration view — with the layout attributes object as its parameter! So we pull out thetitle
and use it as the text of our label:It's easy to see how you might extend the example to make such label features as font and height customizable. Since we are subclassing UICollectionViewFlowLayout, some further modifications might also be needed to make room for the decoration view by pushing down the other elements. Also, technically, we should override
isEqual(_:)
in MyTitleView to differentiate between different titles. All of that is left as an exercise for the reader.