Border๋Š” ๋ฐ”๊นฅ์œผ๋กœ ๊ทธ๋ ค์งˆ๊นŒ ์•ˆ์ชฝ์œผ๋กœ ๊ทธ๋ ค์งˆ๊นŒ? ์˜คํ•ด์™€ ์‚ฝ์งˆ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

Answer

  • innerborder๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ˜„์žฌ ํ‘œํ˜„๋˜๋Š” frame์„ border width๋งŒํผ ์ค„์—ฌ๋ฒ„๋ฆฌ๋ฉด ๋œ๋‹ค.
  • 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()
}
  • ๋‹ค์Œ์˜ View๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘์‹œ์ผœ๋ณธ ๊ฒฐ๊ณผ, Border๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ inner๋กœ ๋“ค์–ด๊ฐ„๋‹ค.
  • ๊ฐ™์€ ํฌ๊ธฐ์ธ ์ƒํ™ฉ์—์„œ border๋ฅผ ๋„ฃ๊ณ  ๋‘ํ™”๋ฉด์„ ๊ฒน์ณ๋ณธ ๊ฒฐ๊ณผ border๊นŒ์ง€ ํฌํ•จํ•œ ํฌ๊ธฐ๋ผ๋Š” ๊ฒƒ์ด ํ™•์ธ๋˜์—ˆ๋‹ค.
    • defaultBorderView: red
    • outerBorderView: blue

์˜คํ•ด

  • inset ๊ด€๋ จ ํ•จ์ˆ˜๋Š” ๋ง ๊ทธ๋Œ€๋กœ frame์„ insetํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด์˜ค๋Š” ํ•จ์ˆ˜์— ๋ถˆ๊ณผํ•˜๋‹ค.
  • border๋Š” inner๋กœ ๊ทธ๋ ค์ ธ, border๊นŒ์ง€ ํฌํ•จํ•œ ํฌ๊ธฐ๋กœ ํ™”๋ฉด์— ๊ทธ๋ ค์ง„๋‹ค.

Reference