Swift / UIView / drawrect – how to get drawrect to update when required

drawrectswiftuiviewupdating

I'm new to learning Swift, and am trying to get an incredibly simple app to run. All I'm trying to do is get UIView.drawRect to update when I press a button. It updates/draws when the app first loads, and then nothing after that, whatever I try. I've been hitting my head against this for a couple of days, and nothing I can find helps.

I created:

  • single view application

  • one button, linked to view controller as an action

  • a new class, Test_View, subclassing UIView

ViewController code:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var f = Test_View()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func Button_Pressed(sender: AnyObject) {
        var f = Test_View()
        f.setNeedsDisplay()
        NSLog("Button pressed.")
    }

}

Test_View code:

class Test_View: UIView {

    override func drawRect(rect: CGRect) {
        let h = rect.height
        let w = rect.width
        var color:UIColor = UIColor.yellowColor()

        var drect = CGRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
        var bpath:UIBezierPath = UIBezierPath(rect: drect)

        color.set()
        bpath.stroke()

        NSLog("drawRect has updated the view")            

    }

}

(Note: the log is updated every time I press the button, so that's not the problem. It's just that the display never changes. Also, I've tried drawing the rectangle with random coordinates, so it's not that it's updating but I'm not seeing it.)

Thanks for any help!

Best Answer

First, you need to specify a designated initializer for UIView (init with frame). Then make your object f a class constant or variable (depending on your need) so it can be accessed within the scope of the project. Also, you must add it as a subview of your view. It looks like this:

import UIKit

class ViewController: UIViewController {
    let f = Test_View(frame: CGRectMake(0, 0, 50, 50))

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(f)
    }

    @IBAction func buttonPressed(sender: UIButton) {
        f.setNeedsDisplay()
    }
}

class Test_View: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {
        let h = rect.height
        let w = rect.width
        let color:UIColor = UIColor.yellow

        let drect = CGRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
        let bpath:UIBezierPath = UIBezierPath(rect: drect)

        color.set()
        bpath.stroke()

        NSLog("drawRect has updated the view")

    }

}
Related Topic