dcbenji
dcbenji

Reputation: 4678

How to Assign a class to a custom UIView in Swift?

I'm trying to create a custom framework so that I can use @IBDesignable to render my view in the storyboard. Normally you can click on the view in the storyboard and select a class from the drop-down in inspector. Since I must assign my custom framework from the dropdown, I now have to create a custom subview within my framework and assign my class too that ((which is a class from graphical circle counter I downloaded from cocoapods JWGCircleCounter). I'm not sure how to do this in S in XCode 6 in Swift. Here is the code for my custom framework so far:

import UIKit
@IBDesignable
class CircleTimer: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        var outerCircle = UIView.class(JWGCircleCounter)(frame CGRectmake: 22, 137, 125, 125)        //This is my guess at how to assign the class, thought of course it doesn't work. I'm also not sure where to delcare the frame after assigning the class
    self.addSubview(outerCircle)
}

Upvotes: 1

Views: 2817

Answers (1)

codester
codester

Reputation: 37189

You should Make the instance of JWGCircleCounter as JWGCircleCounter is subclass of UIView. As JWGCircleCounter has intializer init(frame:CGRect) you can call this to make instance.

Also in swift

class CircleTimer: UIView {
override func layoutSubviews() {
    super.layoutSubviews()
    var outerCircle:JWGCircleCounter = JWGCircleCounter(frame: CGRectMake(22, 137, 125, 125))       //This is my guess at how to assign the class, thought of course it doesn't work. I'm also not sure where to delcare the frame after assigning the class
    self.addSubview(outerCircle)
}

JWGCircleCounter is subclass of UIView and it already override init(frame:) so you can call this to make an instance.

And note you should always pass instance of sub class(JWGCircleCounter) to super class( UIView) not vice versa.Refer Polymorphism

EDIT : For cocoa pods you can try

Select your Project file (Blue on the top right) and go into Build Settings. Then, find 'Allow non-modular includes in Framework Modules' and set it to YES, for both the Project file (blue) and the Custom Framework target

Upvotes: 1

Related Questions