ios - 自定义 UIView 背景图片

标签 ios swift uiview

我正在为我的项目使用 Swift。创建了一个自定义 UIView 类,如下所示:

class AppBgView: UIView {

    override init (frame : CGRect) {
        super.init(frame : frame)
        //self.isMemberOfClass(<#aClass: AnyClass#>)
    }

    convenience init () {
        self.init(frame:CGRect.zeroRect)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
    }
}

尝试在所需的 init() 方法中设置背景图像。但背景图像似乎没有改变。任何建议将不胜感激。谢谢。

最佳答案

在每个方法中放置一个断点,看看调用了哪个构造函数。 您将需要移动线路

self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)

到被调用的构造函数。

在 Swift 中,一个类必须至少有一个指定的初始化器。 您可能需要从 init() 中删除单词convenience

试一试: UIColor(patternImage:UIImage(named: "BGround.png")!).CGColor

关于ios - 自定义 UIView 背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33613923/

相关文章:

ios - 如何在 ios 中为 UiView 添加顶部或底部边框?

ios - 如何将 CALayer 子类化以用作另一个 CALayer 掩码?

iphone - UIView 大小/位置在 iPad 上不像在 iPhone 上那样工作

ios - 打开 cv 划痕检测

ios - GameCenter 通知干扰 iAd View

swift - <<错误类型>>奇怪的错误

arrays - 将类型数组转换为协议(protocol)时出现 fatal error : cannot be bridged from Objective-C

滚动时,iOS UITableView 滚动条位于表头下方

swift - 无法使用 Swift 包管理器

iphone - UIView 没有填满它的框架