Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background image stretch vertically, but repeating horizontally

Tags:

ios

swift

Currently using:

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

But the problem is that it repeats both vertically and horizontally. I'm trying to keep it repeating horizontally while stretching vertically to fit the screen.

like image 847
eskimo Avatar asked Nov 08 '22 23:11

eskimo


1 Answers

Subclass UIView call it something like BackgroundImageView or something it override drawInRect thusly:

import UIKit
@IBDesignable
class BackgroundImageView: UIView {
    @IBInspectable var backgroundImage:UIImage?{
        didSet{
            self.setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
        var i:CGFloat = 0.0
        if backgroundImage != nil
        {
            while (i*backgroundImage!.size.width)<self.bounds.size.width
            {
                backgroundImage!.drawInRect(CGRect(x: i*backgroundImage!.size.width, y: 0.0, width: backgroundImage!.size.width, height: self.bounds.size.height))
                i+=1.0
            }
        }
    }


}

Drag out a UIView in IB change its class to BackgroundImageView and set the backgroundImage to background.png.

like image 139
beyowulf Avatar answered Nov 14 '22 22:11

beyowulf