Border는 바깥으로 그려질까 안쪽으로 그려질까? 오해와 삽질에 대해 정리해본다.
inset(by:)
, insetBy(dx:dy:)
를 사용하여 처리할 수 있다.
import UIKit
internal final class InnerBorderViewController: UIViewController {
internal override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.defaultBorderView.backgroundColor = .red
self.outerBorderView.backgroundColor = .blue
self.innerBorderView.backgroundColor = .blue
self.defaultBorderView.alpha = 0.5
self.outerBorderView.alpha = 0.5
self.defaultBorderView.frame = CGRect(origin: CGPoint(x: 50, y: 100),
size: CGSize(width: 100, height: 100))
self.outerBorderView.frame = CGRect(origin: CGPoint(x: 50, y: 100),
size: CGSize(width: 100, height: 100))
self.innerBorderView.frame = CGRect(origin: CGPoint(x: 50, y: 400),
size: CGSize(width: 100, height: 100))
self.outerBorderView.layer.borderWidth = 2
self.innerBorderView.layer.borderWidth = 2
self.view.addSubview(self.defaultBorderView)
self.view.addSubview(self.outerBorderView)
self.view.addSubview(self.innerBorderView)
// inset(by:)
self.innerBorderView.frame = self.innerBorderView.frame.inset(by: UIEdgeInsets(top: 2,
left: 2,
bottom: 10,
right: 10))
// insetBy(dx:dy:)
// self.innerBorderView.frame = self.innerBorderView.frame.insetBy(dx: 2, dy: 2)
}
private let defaultBorderView = UIView()
private let outerBorderView = UIView()
private let innerBorderView = UIView()
}