I have added a subview with its own UIView class LogoLineView.swift to the superview ViewController.swift.
Inside my superview in my storyboard I have 3 UI elements:
- a label with some colored text to simulate a logo "Hello World"
- then, I have my subview of about 5 points in height and as wide as can be where I intend to draw a horizontal line that is half blue and half red
- finally, I have another label with just some gray text
Now, in my subview class (LogoLineView.swift) I am overriding the drawRect as shown below:
class LogoLineView: UIView {
var lineWidth: CGFloat = 0.5 {didSet { setNeedsDisplay() } }
var blueLineColor: UIColor = UIColor.blueColor() { didSet { setNeedsDisplay() } }
var redLineColor: UIColor = UIColor.redColor() { didSet { setNeedsDisplay() } }
var subViewCenter: CGPoint {
return convertPoint(center, fromView: superview)
}
override func drawRect(rect: CGRect) {
var blueLinePath = UIBezierPath()
blueLinePath.moveToPoint(subViewCenter)
blueLinePath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.midY))
blueLinePath.lineWidth = lineWidth;
blueLineColor.set()
blueLinePath.stroke()
var redLinePath = UIBezierPath()
redLinePath.moveToPoint(subViewCenter)
redLinePath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.midY))
redLinePath.lineWidth = lineWidth;
redLineColor.set()
redLinePath.stroke()
}
}
The problem I am having is that when I add constraints to the subview in relation to the other elements in the superview, my drawn line is not visible.
However, if I remove all the other elements in my superview and leave only the subview, then I am able to see my line drawn just the way I want it.
I want my line drawn in between the text "Hello World" and the text "This is my first time using Core Graphics and Bezier Paths". And I want this line to be able to adapt to any screen size.
The constraints I am using are below:
- "Hello World" constraints:
- Subview to draw line constraints:
- grey text "This is my first time using Core Graphics and Bezier Path" constraints:
could you please help me find what's missing? I am very new to using Core Graphics and Bezier Paths. Thank you.