Clicky

iOS Dev Nugget 285 maskedCorners

.

Need to run a code review on your codebase? Hire me

This is super handy for implementing rounded corners when performance doesn't matter:

aView.clipsToBounds = true
aView.layer.cornerRadius = 20

But sometimes, maybe the design requires rounding only the top 2 corners. If it's an almost full-screen view, a simple way to do it is to let a view obscure the bottom 2 corners or just make sure it is offscreen (again, if performance is not an issue). But in iOS 11, there's a new property:

var maskedCorners: CACornerMask

This lets you set specific (corners) to mask. Here's some playground code:

import UIKit
import PlaygroundSupport

class VC: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)
        view.backgroundColor = .black

        let square = UIView(frame: CGRect(x: 20, y: 20, width: 200, height: 200))
        square.backgroundColor = .white
        square.layer.cornerRadius = 20
        square.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        view.addSubview(square)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

PlaygroundPage.current.liveView = VC()

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues