Overriding delegate property of UIScrollView in Swift (like UICollectionView does)

UIScrollView has a delegate property witch conforms to UIScrollViewDelegate

protocol UIScrollViewDelegate : NSObjectProtocol {
    //...
}
class UIScrollView : UIView, NSCoding {
    unowned(unsafe) var delegate: UIScrollViewDelegate?
    //...
}

UICollectionView overrides this property with a different type UICollectionViewDelegate

  • Is it possible to have a dictionary with a mutable array as the value in Swift
  • NSDate timeIntervalSince1970 not working in Swift?
  • Waiting for multiple blocks to finish
  • Proximity sensor in Swift (from Objective-C)
  • NSInvalidUnarchiveOperationException: 'Could not instantiate class named _UITableViewCellSeparatorView
  • The joys of didFailLoadWithError UIWebview
  • protocol UICollectionViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
       //...
    }
    
    class UICollectionView : UIScrollView {
         unowned(unsafe) var delegate: UICollectionViewDelegate?
       //...
    }
    

    When I try to override UIScrollViews delegate with my protocol like so :

    protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
        //...
    }
    
    class MyScrollView: UIScrollView {
        unowned(unsafe) var delegate: MyScrollViewDelegate?
    
    }
    

    compiler gives me two warnings:

    • Property ‘delegate’ with type ‘MyScrollViewDelegate?’ cannot override a property with type ‘UIScrollViewDelegate?’
    • ‘unowned’ cannot be applied to non-class type ‘MyScrollViewDelegate?’

    How can I subclass UIScrollView and override type of delegate property (e.i. use custom delegate protocol) ?

    5 Solutions Collect From Internet About “Overriding delegate property of UIScrollView in Swift (like UICollectionView does)”

    I think overriding an inherited property is something that’s possible in Objective-C but not (at least currently) in Swift. The way I’ve handled this is to declare a separate delegate as a computed property of the correct type that gets and sets the actual delegate:

    @objc protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol {
        func myHeight() -> CGFloat
        // ...
    }
    
    class MyScrollView: UIScrollView {
        var myDelegate: MyScrollViewDelegate? {
            get { return self.delegate as? MyScrollViewDelegate }
            set { self.delegate = newValue }
        }
    }
    

    This way anything that calls the scroll view delegate normally still works, and you can call your particular delegate methods on self.myDelegate, like this:

    if let height = self.myDelegate?.myHeight() {
        // ...
    }
    

    Here is a solution for changing the type of the overriding properties in Swift. It is especially useful when you need to extend protocols of delegates.

    @objc protocol ExtendedUIScrollViewDelegate: UIScrollViewDelegate {
         func someNewFunction()
    }
    
    class CustomScrollView: UIScrollView {
    
        weak var delegateInterceptor: ExtendedScrollViewDelegate?
        override var delegate: UIScrollViewDelegate! {
            didSet {
                if let newValue = delegate {
                    let castedDelegate = unsafeBitCast(delegate, ExtendedScrollViewDelegate.self)
                    delegateInterceptor = castedDelegate
                }
                else {
                    delegateInterceptor = nil
                }
            }
        }
    }
    

    This works as tested with Swift version 1.2. I hope it helps.

    You can do like this:

    protocol ExtendedUIScrollViewDelegate: UIScrollViewDelegate {
        func someNewFunction()
    }
    
    class CustomScrollView: UIScrollView {
    
        weak var myDelegate: ExtendedScrollViewDelegate?
        override weak var delegate: UIScrollViewDelegate? {
            didSet {
                myDelegate = delegate as? ExtendedScrollViewDelegate
            }
        }
    }
    

    Hope this helps

    My favoured method personally is not to subclass scrollviews directly but to make a UIView subclass containing and acting as delegate for a separate scrollview, then forward that scrollview’s delegate messages on to the UIView subclass’s own delegate where necessary. This also allows for the adding of custom controls outside of the area defined by the scroll view. It may seem a little inelegant compared to a direct subclass, but it does at least avoid unpleasant hacks.

    You can override get and set method by declare function like:

    func setDelegate(delegate:UITableViewDelegate?){
         self.delegateInterceptor = delegate;
    }
    

    swift compiler the property to method as Objective-c does.