UIView background pattern

Can I achieve the same value as:

view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]]; 

using only IB. No need to write such a codelink? It seems that IB only allows you to customize a simple color, and not as a wallpaper.

+7
source share
3 answers

The Builder interface does not allow the use of a wallpaper. However, you can simply overlay the background on the corresponding image in the UIImageView. This will give the desired look of the application without using the code that you specified above.

However, I strongly disagree that you should only use Interface Builder. The code is much cleaner in my eyes and allows a lot more control over what you are building.

+6
source

This cannot be done in IB, but the code is correct. Despite the fact that others are commenting, use IB as much as possible, so you can focus your efforts on the real business logic of your application by only setting up further where IB does not reach. Good luck

+2
source

As others have said, you cannot do this solely in IB / storyboard. But with @IBDesignable, you can add a few lines to your subclass and show it in IB, which is pretty cool.

 import UIKit @IBDesignable class MyView: UIView { override func awakeFromNib() { super.awakeFromNib() self.addPattern() } override func prepareForInterfaceBuilder() { self.addPattern() } private func addPattern() { let image = UIImage(named: "MyPattern", in: Bundle(for: self.dynamicType), compatibleWith: nil) self.backgroundColor = UIColor(patternImage: image!) } } 

Then just change the storyboard / IB class to MyView.

0
source

All Articles