LoginSignup
1
0

More than 1 year has passed since last update.

Interface Builder 上で maskedCorners を設定

Posted at

背景

iOS11 より、 View の四隅を丸める際に maskedCorners を指定することで、一部だけに cornerRadius を設定することが可能となりました。

コード上での設定するとなると

view.layer.cornerRadius = 16
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXminYCorner]

となるのですが、レイアウトを Interface Builder で組んでいる場合、このためだけにコード側で処理するのは面倒なため、 Interface Builder 上で maskedCorners の設定が完結することを目指します。

設定方法

Interface Builder 上の User Defined Runtime Attributes にて、 layer.maskedCorners の Key Path に対して、 15 までの Number を設定することで対応が可能となります。

IB 上の設定 表示
image.png image.png

maskedCorners の型である CACornerMaskprotocol OptionSet に準拠することによって、複数のフラグ設定を UInt のビットフラグ表現で保持できるような実装となっています。

ビットフラグとしては、

  • layerMinXMinYCorner (左上)
  • layerMaxXMinYCorner (右上)
  • layerMinXMaxYCorner (左下)
  • layerMaxXMaxYCorner (右下)

の4つを取るため、 $0 〜 (2^4 - 1)$ の値によって、 maskedCorners: CACornerMask を表現できます。

早見表

以下に maskedCorners: CACornerMask に数値を設定する場合の対応をまとめました。

右下
MaxXMaxY
左下
MinXMaxY
右上
MaxXMinY
左上
MinXMinY
表示
0 00.png
1 :o: 01.png
2 :o: 02.png
3 :o: :o: 03.png
4 :o: 04.png
5 :o: :o: 05.png
6 :o: :o: 06.png
7 :o: :o: :o: 07.png
8 :o: 08.png
9 :o: :o: 09.png
10 :o: :o: 10.png
11 :o: :o: :o: 11.png
12 :o: :o: 12.png
13 :o: :o: :o: 13.png
14 :o: :o: :o: 14.png
15 :o: :o: :o: :o: 15.png
1
0
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
1
0