LoginSignup
12
11

More than 5 years have passed since last update.

UIButtonの角丸・枠線の設定をユーティリティエリアから編集できるようにする

Posted at

UIButtonに角丸や枠線の色を変えたい場合に、コードからではなくユーティリティエリアから設定したい。
StoryBoardですぐ確認したい。

UIButtonのサブクラスを作る

RoundedButton.swift
@IBDesignable class RoundedButton: UIButton {

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

    override func draw(_ rect: CGRect) {
        layer.cornerRadius = cornerRadius
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor
        clipsToBounds = true
    }
}

StoryBoardより設定する

StoryBoardよりボタンを選択して、作成したクラスを設定する
スクリーンショット 2017-07-17 12.23.29.png
パラメータが設定できるようになる
スクリーンショット 2017-07-17 12.23.54.png

ボタンに限らず他にも色々使えそうです。

12
11
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
12
11