본문 바로가기

iOS/Swift

[ios - swift] UIView several corner radius

UIView에 특정 코너에 radius 설정하기

//label.roundCorners([.topLeft, .topRight], radius: 10)

import Foundation
import UIKit


extension UIView {
   
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        if #available(iOS 11.0, *) {
            clipsToBounds = true
            layer.cornerRadius = radius
            layer.maskedCorners = CACornerMask(rawValue: corners.rawValue)
        } else {
            let path = UIBezierPath(
                roundedRect: bounds,
                byRoundingCorners: corners,
                cornerRadii: CGSize(width: radius, height: radius)
            )
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            layer.mask = mask
        }
    }
}

'iOS > Swift' 카테고리의 다른 글

[ios - swift] google MLKit - Text Recognition  (0) 2022.12.06
[ios - swift] custom camera  (0) 2022.12.06
[ios - swift] download image from url  (0) 2022.12.05
[ios - swift] Alamofire upload image data  (0) 2022.12.05
[ios - swift] resize UIImage  (0) 2022.12.02