LoginSignup
13
14

More than 5 years have passed since last update.

UIの上だけ角丸にする実装

Last updated at Posted at 2016-03-28

Buttonの上だけ角丸にしたかったので、いろいろ試してみました。
以下のExtensionを追加してください。

hoge.swift
extension CAShapeLayer {

    // UIの上だけ角丸
    class func drawTopCornerRadius(radius: CGFloat, ui:AnyObject) -> CAShapeLayer {
        let corners: UIRectCorner = [.TopLeft, .TopRight]
        let maskPath = UIBezierPath(roundedRect: ui.bounds,
                                            byRoundingCorners: corners,
                                            cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = ui.bounds
        maskLayer.path = maskPath.CGPath
        return maskLayer
    }
}

角丸にしたいUIの .layer.mask に渡してあげるとできました。

HogeController.swift
"角丸にしたいUI".layer.mask = CAShapeLayer.drawTopCornerRadius(10.0, ui: "角丸にしたいUI")
13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14