Reputation: 1699
While debugging the View Hierarchy, I came across this optimization warning about a UISwitch
control with a mask view. Though I have no clear picture what would be the benefits of addressing this warning (insights please), I tried to fix it with no success.
Optimization Opportunities: The layer is using a simple layer with background color set as a mask. Instead, use a container layer of the same
frame
andcornerRadius
as the mask, but withmasksToBounds
set to YES.
let maskedSwitch: UISwitch = UISwitch()
maskedSwitch.translatesAutoresizingMaskIntoConstraints = false
maskedSwitch.tintColor = .black
maskedSwitch.onTintColor = .green
maskedSwitch.backgroundColor = .black
let maskView = UIView(frame: maskedSwitch.frame)
maskView.backgroundColor = .black
maskView.layer.cornerRadius = maskedSwitch.frame.height / 2
maskView.layer.masksToBounds = true
maskView.clipsToBounds = true
maskedSwitch.mask = maskView
let scale: CGFloat = 2.5 / 3
maskedSwitch.transform = CGAffineTransform(scaleX: scale, y: scale)
As suggested, I tried removing the maskView and directly applying the cornerRadius and offTintColor on switch. But it ended up messing the scale transformation, and it is kind of important to retain the UI.
How can I further optimise the code and make sure all these conditions addressed: 1. set offTintColor to black, 2. scale the switch and 3. Avoid the optimization opportunity warning?
Note: Please do share if there is an Apple doc to study more on the topic Optimization Opportunities
. Thanks!
Upvotes: 1
Views: 362