LoginSignup
1

More than 3 years have passed since last update.

swiftで角丸のボタンを実装する方法

Posted at

はじめに

今回も備忘録として残します。

コード実装

test.swift
import UIKit

@IBDesignable class RoundedCornersButton: UIButton {

    // 角丸の半径(0で四角形)
    @IBInspectable var cornerRadius: CGFloat = 0.0

    // 枠
    @IBInspectable var borderColor: UIColor = UIColor.clear
    @IBInspectable var borderWidth: CGFloat = 0.0

    override func draw(_ rect: CGRect) {
        // 角丸
        self.layer.cornerRadius = cornerRadius
        self.clipsToBounds = (cornerRadius > 0)

        // 枠線
        self.layer.borderColor = borderColor.cgColor
        self.layer.borderWidth = borderWidth

        super.draw(rect)
    }
}

実装レイアウトに反映する方法

「show the identity inspector」の「custom class」に先ほど作ったClass名を入れる

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
1