从自己的UView SubClass中添加UIView作为SuperView子视图

时间:2015-06-02 14:28:56

标签: ios uiview subclass addsubview superview

实际上我认为这个很简单,而且我错过了一些东西。

我正在创建UIView的子类,而不是像ViewController那样将它添加为子视图:

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

我正在尝试从子类添加它,这是我到目前为止所尝试的:

class RateFrameWork: UIView 

1)来自super init编码器(剂量工作)

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

    }

2)从init方法(剂量工作)

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)

有什么建议吗? 如何将其作为子视图的子视图添加?

1 个答案:

答案 0 :(得分:2)

你不能这样做,因为这与这个想法相矛盾。

视图没有superview,除非它被添加为子视图,并且视图不知道它将在什么情况下何时被添加为子视图。

可以在keyWindow添加一些特殊视图(例如AlertView,ActionView),但不能添加视图。

相关问题