小编Ale*_*rov的帖子

Swift:对UITextView或UICollectionView进行子类化并进行适当的初始化

子类化UITextView(和UICollectionView)的问题是指定的构造函数是"initWithFrame".但在现实生活中,当它从故事板加载时,将调用initWithCoder.

class BorderedTextView: UITextView {
    //will be called
    init(coder: NSCoder?){
       //constant values here is not an option
       super.init(frame: CGRectMake(0,0,100,100), textContainer: nil)
    }
    //will not be called
    init(frame: CGRect, textContainer: NSTextContainer!) {
        super.init(frame: frame, textContainer: textContainer)
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,我无法在init上调用任何UI自定义代码,并为除默认值之外的Swift变量提供任何初始化值.

我想通过从"编码器"中提取帧大小可以暂时解决问题,但我没有找到它的关键.

任何想法比硬编码帧值更好?

uitextview uiview ios swift

9
推荐指数
1
解决办法
8774
查看次数

标签 统计

ios ×1

swift ×1

uitextview ×1

uiview ×1