Reputation: 531
I'm using Swift
for my project. Created a custom UIView
class like the following:
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")!)
}
}
trying to set background image inside required init()
method. But the background image doesn't seems to change. Any suggestion would be appreciated. Thanks.
Upvotes: 0
Views: 2665
Reputation: 99
SWIFT 5
self.contentMode = UIView.ContentMode.scaleToFill
self.layer.contents = UIImage(named:"bground.png")?.cgImage
self.bringSubviewToFront(self)
self.clipsToBounds = true
Upvotes: 0
Reputation: 531
Finally I manage to solve it. replaced the self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
code with this one:
if let image = UIImage(named: "bground.png") {
self.layer.backgroundColor = UIColor(patternImage: image).CGColor
}
The background image was added to the layer of that view. It works. Thanks everyone.
Upvotes: 0
Reputation: 8629
Put a breakpoint in each one of this methods and see which one of the constructors is being called. You will need to move the line
self.backgroundColor = UIColor(patternImage: UIImage(named: "BGround.png")!)
To the constructor that being called.
In Swift A class must have at least one designated initializer. You may need to remove the word convenience from init().
Give this one a shot: UIColor(patternImage:UIImage(named: "BGround.png")!).CGColor
Upvotes: 1