Anyone knows what should I do to achieve the follo...
# kotlin-native
c
Anyone knows what should I do to achieve the following in kotlin?
Copy code
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attributes = super.layoutAttributesForElements(in: rect)

        var leftMargin = sectionInset.left
        var maxY: CGFloat = -1.0
        attributes?.forEach { layoutAttribute in
            if layoutAttribute.frame.origin.y >= maxY {
                leftMargin = sectionInset.left
            }

            layoutAttribute.frame.origin.x = leftMargin

            leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
            maxY = max(layoutAttribute.frame.maxY , maxY)
        }

        return attributes
    }
}
Asking this because if I try to do:
Copy code
class LeftLayout: UICollectionViewFlowLayout {
    constructor(): super()

    override fun layoutAttributesForElementsInRect(rect: CGSize){
        
    }
}
It simply tells me that theres nothing to override..
r
Your swift function takes a parameter
rect: CGRect
but your kotlin takes
rect: CGSize
c
Yeah, either way it is still telling me that there’s nothing to be overriden
“layoutAttributesForElementsInRect” overrides nothing
r
I don't know that specific function but it might be that the name is slightly different than you expect, depending on how it's translating through objective-c
c
Looking at here it seems that it doesnt have that function..
Copy code
@kotlinx.cinterop.ExternalObjCClass public open class UICollectionViewFlowLayout : platform.UIKit.UICollectionViewLayout {
    public companion object : platform.UIKit.UICollectionViewFlowLayoutMeta, kotlinx.cinterop.ObjCClassOf<platform.UIKit.UICollectionViewFlowLayout> {
    }

    @kotlinx.cinterop.ObjCConstructor public constructor() { /* compiled code */ }

    @kotlinx.cinterop.ObjCConstructor public constructor(coder: platform.Foundation.NSCoder) { /* compiled code */ }

    public final var estimatedItemSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> /* compiled code */

    public final var footerReferenceSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> /* compiled code */

    public final var headerReferenceSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> /* compiled code */

    public final var itemSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> /* compiled code */

    public final var minimumInteritemSpacing: platform.CoreGraphics.CGFloat /* = kotlin.Double */ /* compiled code */

    public final var minimumLineSpacing: platform.CoreGraphics.CGFloat /* = kotlin.Double */ /* compiled code */

    public final var scrollDirection: platform.UIKit.UICollectionViewScrollDirection /* compiled code */

    public final var sectionFootersPinToVisibleBounds: kotlin.Boolean /* compiled code */

    public final var sectionHeadersPinToVisibleBounds: kotlin.Boolean /* compiled code */

    public final var sectionInset: kotlinx.cinterop.CValue<platform.UIKit.UIEdgeInsets> /* compiled code */

    public final var sectionInsetReference: platform.UIKit.UICollectionViewFlowLayoutSectionInsetReference /* compiled code */

    @kotlinx.cinterop.ObjCMethod public open external fun estimatedItemSize(): kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun footerReferenceSize(): kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun headerReferenceSize(): kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> { /* compiled code */ }

    @kotlin.Deprecated @kotlinx.cinterop.ObjCMethod @kotlinx.cinterop.internal.CCall.ConsumesReceiver @kotlinx.cinterop.internal.CCall.ReturnsRetained public open external fun init(): platform.UIKit.UICollectionViewFlowLayout { /* compiled code */ }

    @kotlin.Deprecated @kotlinx.cinterop.ObjCMethod @kotlinx.cinterop.internal.CCall.ConsumesReceiver @kotlinx.cinterop.internal.CCall.ReturnsRetained public open external fun initWithCoder(coder: platform.Foundation.NSCoder): platform.UIKit.UICollectionViewFlowLayout? { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun itemSize(): kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize> { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun minimumInteritemSpacing(): platform.CoreGraphics.CGFloat /* = kotlin.Double */ { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun minimumLineSpacing(): platform.CoreGraphics.CGFloat /* = kotlin.Double */ { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun scrollDirection(): platform.UIKit.UICollectionViewScrollDirection { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun sectionFootersPinToVisibleBounds(): kotlin.Boolean { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun sectionHeadersPinToVisibleBounds(): kotlin.Boolean { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun sectionInset(): kotlinx.cinterop.CValue<platform.UIKit.UIEdgeInsets> { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun sectionInsetReference(): platform.UIKit.UICollectionViewFlowLayoutSectionInsetReference { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setEstimatedItemSize(estimatedItemSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize>): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setFooterReferenceSize(footerReferenceSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize>): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setHeaderReferenceSize(headerReferenceSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize>): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setItemSize(itemSize: kotlinx.cinterop.CValue<platform.CoreGraphics.CGSize>): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setMinimumInteritemSpacing(minimumInteritemSpacing: platform.CoreGraphics.CGFloat /* = kotlin.Double */): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setMinimumLineSpacing(minimumLineSpacing: platform.CoreGraphics.CGFloat /* = kotlin.Double */): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setScrollDirection(scrollDirection: platform.UIKit.UICollectionViewScrollDirection): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setSectionFootersPinToVisibleBounds(sectionFootersPinToVisibleBounds: kotlin.Boolean): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setSectionHeadersPinToVisibleBounds(sectionHeadersPinToVisibleBounds: kotlin.Boolean): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setSectionInset(sectionInset: kotlinx.cinterop.CValue<platform.UIKit.UIEdgeInsets>): kotlin.Unit { /* compiled code */ }

    @kotlinx.cinterop.ObjCMethod public open external fun setSectionInsetReference(sectionInsetReference: platform.UIKit.UICollectionViewFlowLayoutSectionInsetReference): kotlin.Unit { /* compiled code */ }
}
a
Hello, @Christian Sousa! This function seems to be represented in Kotlin as
Copy code
@kotlinx.cinterop.ObjCMethod public external fun platform.UIKit.UICollectionViewLayout.layoutAttributesForElementsInRect(rect: kotlinx.cinterop.CValue<platform.CoreGraphics.CGRect>): kotlin.collections.List<*>? { /* compiled code */ }
and can be imported directly as
import platform.UIKit.layoutAttributesForElementsInRect
.
c
@Artyom Degtyarev [JB] thanks for the answer. Do you know why I can’t override it inside a subclass of UICollectionViewFlowLayout?
I think I already tried everything I could think of to override it..
a
This issue seems related(https://github.com/JetBrains/kotlin-native/issues/1035). AFAIK the best option you have is to shadow this extension function with your own, same-named one.
c
Thank you! Will try that 🙂