Reputation: 8944
I have created a custom UIView.I am making it's some part of color as other color using below code.
import Foundation
class BottomView:UIView {
override func draw(_ rect: CGRect) {
self.fillColor(with: .gray, width: 20)
}
func fillColor(with color:UIColor,width:CGFloat) {
let topRect = CGRect(x:0, y:0, width : width, height: self.bounds.height);
color.setFill()
UIRectFill(topRect)
}
}
Now my view has some initial part of color as gray & rest of its color is different. Now from controller if i try to change it's color to green the it is not changed from start position. It's color changed after inital color.
Please let me know how i can completely set background color to green.
Upvotes: 1
Views: 2189
Reputation: 24341
You can use setNeedsDisplay
on your bottom view
and redraw it when setting the backgroundColor
.
setNeedsDisplay()
Marks the receiver’s entire bounds rectangle as needing to be redrawn.
You can use this method or the setNeedsDisplay(_:) to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.
Example:
1. BottomView
class
class BottomView:UIView
{
var showGray = true
override func draw(_ rect: CGRect)
{
if showGray
{
self.fillColor(with: .gray, width: 20)
}
}
func fillColor(with color:UIColor,width:CGFloat)
{
let topRect = CGRect(x:0, y:0, width : width, height: self.bounds.height);
color.setFill()
UIRectFill(topRect)
}
}
2. In your UIViewController
self.bottomView.showGray = false
self.bottomView.setNeedsDisplay()
self.bottomView.backgroundColor = .green
Upvotes: 2