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

标签 ios swift uiview core-graphics

我将 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
您正在刷新类而不是类的实例。

@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))

关于ios - 如何在 Swift 中硬编码 UIView 的大小属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200273/

相关文章:

ios - 如何使用 Sprite Kit Physics 创建一个 "cross"(或 "plus")形状的旋转对象?

ios - Swift 3 Xcode 8 中的 FileManager 和 urlsForDirectory 错误

swift - For 循环中的错误 - Swift 语言

ios - 给 UIView 添加阴影

ios - Subview动画受mapview影响

swift - 关闭 UIAlertController 后如何重新加载 UIView

iphone - iOS - 将 NSDictionary 写入 Localized.strings

iOS 多个弹跳球

IOS Swift 处理全局事件

swift - 进入前台时表reload.data