Search code examples
iosuiviewsubclassaddsubviewsuperview

Add UIView as SuperView subView from its own UView SubClass


Actually I think this one is very simple, and I'm missing something.

I'm creating Subclass of UIView, and instead of adding it as a subView from the ViewController like this :

rateus = RateFrameWork(AppID: "asdasds", BackGroundColor: UIColor.blueColor())
self.addSubView(rateus)

I'm trying adding it from the subclass, this is what I tried so far :

class RateFrameWork: UIView 

1) From the super init coder(dosent work)

 required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        superview?.addSubview(self)

    }

2) From the init method(dosent work)

init?(AppID: String!, BackGroundColor: UIColor!)
super.init(frame: CGRect(x:  0 , y: 0, width: UIScreen.mainScreen().bounds.size.width , height:UIScreen.mainScreen().bounds.size.height))
 superview?.addSubview(self)

Any suggestions? How can I add it as subview from its own subclass?


Solution

  • You can't do it because this is contradictory to the idea.

    A view does not have superview unless it's to be added as subview, and a view does not know that it would be added as a subview under what circumstances and when.

    Some special views(e.g. AlertView、ActionView) can be added to the keyWindow but your view not.