如何在 Swift 中硬编码 UIView 的大小属性

How to hardcode size attributes of UIView in Swift

我将 UIView 子类化为条形(制作条形图),如何对 FirstBarView 的大小属性进行硬编码?我收到错误:

FirstBarView type does not have a member named frame?

import UIKit





    @IBDesignable class FirstBarView: UIView {


        @IBInspectable var FirstBarViewColor: UIColor = UIColor.orangeColor()

        override func drawRect(rect: CGRect) {

        FirstBarView.frame = CGRectMake(50, 200, 20, 400)

        }
    }

使用 self 而不是 FirstBarView
您正在刷新 class 而不是 class.

的实例
@IBDesignable class FirstBarView: UIView {        
    @IBInspectable var FirstBarViewColor: UIColor = UIColor.orangeColor()
    override func drawRect(rect: CGRect) {
        self.frame = CGRectMake(50, 200, 20, 400)
    }
}


//To create a new class with a rect.

var myFirstBarView = FirstBarView()
myFirstBarView.drawRect(CGRectMake(0,0,200,50))