10

I'm implementing a custom PopoverBackgroundView, and as specified at Swift documentation I gotta implement the methods as follow:

class SettingsPopoverBackgroundView: UIPopoverBackgroundView {

override var arrowOffset: CGFloat {
    get {
        return 0.0
    }
    set {
        super.arrowOffset = newValue
    }

}

override var arrowDirection: UIPopoverArrowDirection {
    get {
        return UIPopoverArrowDirection.Up
    }
    set {
        super.arrowDirection = newValue
    }
}

func contentViewInsets() -> UIEdgeInsets {
    return UIEdgeInsetsMake(10, 10, 10, 10)
}

func arrowBase() -> CGFloat {
    return 2.0
}

func arrowHeight() -> CGFloat {
    return 2.0
}
}

However, I'm still getting the error:

UIPopoverBackgroundView contentViewInsets must be implemented by subclassers.

It seems like Apple has some garbled exception as for this subclassing, as can be seen here, because I do implement contentViewInsets, but still get the error.

This is how I am settings the background class to the popover in the prepareForSegue method:

popover.popoverBackgroundViewClass = SettingsPopoverBackgroundView.self

Is it right?

Can anyone see what am I doing wrong?

Community
  • 1
  • 1
andriosr
  • 481
  • 4
  • 12
  • I copied your code, and I don't get any error. Are you getting this error during the build or at run time? – rdelmar Apr 05 '15 at 17:02
  • Just to make sure you are actually overwriting the correct methods: add the `overwrite` keyword to the last 3 functions as well. – luk2302 Apr 05 '15 at 17:02
  • @luk2302 isn't it override? I think there is no overwrite keywork, and it's not possible to override those because they are part of the protocol: >UIPopoverBackgroundViewMethods, so I am forced to implement them anyway. – andriosr Apr 05 '15 at 19:13
  • Yeah, override, my brain is pushing out scrambled stuff due to head cold - anyway: cant really help you there :/ – luk2302 Apr 05 '15 at 19:54
  • I updated with the way I am assigning the background class to the popover, not sure if it is right. I have to translate some code from objective-C, once I didn't find it in Swift – andriosr Apr 05 '15 at 19:58

1 Answers1

14

contentViewInsets(), arrowBase(), and arrowHeight() are all class functions, so you need "override class" in front of func for those.

For arrowOffset and arrowDirection, the docs say that your methods must not call super. I'm not sure exactly what you need to put in those setters (that depends on what you're trying to do), but if you leave them blank, the app should run with no errors (though you won't see an arrow).

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thank you a lot! I got another error, but surely that solved the first one. The new error is: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key settingsView. if u could help as well :) – andriosr Apr 05 '15 at 20:26
  • 2
    @andriosr That error is usually caused by referring to something, "settingsView" in this case, that doesn't exist in your code. You should check in your storyboard (or xibs if you have them) for some place where you used settingsView. – rdelmar Apr 05 '15 at 20:29