简体   繁体   中英

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?

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(eg AlertView、ActionView) can be added to the keyWindow but your view not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM