์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- UIKit

(iOS) UITabBar, UINavigationBar shadow ์ œ๊ฑฐ, border ์ œ๊ฑฐ

  • -

2023. 4. 25.

 

 

 

๋ฌธ์ œ

Custom TabBar์™€ NavigationBar ๋ฅผ ๋งŒ๋“ค์ผ์ด ์ƒ๊ฒผ๋Š”๋ฐ VC์™€ ๋‹ฟ๋Š”๋ถ€๋ถ„์— ์ž๊พธ Border? Shadow? ๊ฐ™์€ ์ค„์ด ํ•˜๋‚˜ ์ƒ๊ธด๋‹ค.
Hierarchy๋ฅผ ๋ณด๋‹ˆ ์ตœ์ƒ์œ„ ๊ณ„์ธต์— ๊นŒ๋งŒ ImageView๊ฐ€ ๋œฌ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ตฌ๊ธ€๋ง์„ ์ข€ ํ•ด๋ณด๋‹ˆ UINavigationBarAppearance ๊ฐ์ฒด์˜ backgroundImage ํ”„๋กœํผํ‹ฐ์™€ shadowImageํ”„๋กœํผํ‹ฐ์— ๋นˆ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์›ํ•˜๋Š”๋Œ€๋กœ ์ƒ‰์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ธ€์„ ๋ณด์•˜๋‹ค.

if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundImage = UIImage()
            appearance.shadowImage = UIImage()
}

์š”๋ ‡๊ฒŒ ๋„ฃ์–ด์ค˜๋ดค๋Š”๋ฐ ์™œ์ธ์ง€ ๊ทธ๋Œ€๋กœ ๊นŒ๋งŒ ImageView๊ฐ€ ๋–ด๋‹ค.

ํ•ด๊ฒฐ

์•„๋งˆ UIImage()๋ฅผ ๋„ฃ์–ด๋„ ์ƒ‰์ƒ๋ณ€ํ™”๋Š” ์—†๊ธฐ ๋–„๋ฌธ์ด ์•„๋‹๊นŒ ์‹ถ์–ด์„œ UIGraphicsGetImageFromCurrentImageContext๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง์ ‘ ๋‹จ์ƒ‰์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ๋‹ค.

์ด๋ฏธ์ง€ ์ƒ์„ฑ

func colorForNavBar(_ color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)

        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? UIImage()
    }

appearance setting

if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundImage = colorForNavBar(color: .white)
            appearance.shadowImage = colorForNavBar(color: .white)
}

TabBar์˜ ๊ฒฝ์šฐ์—๋„ UINavigationBarAppearance ๊ฐ€ ์•„๋‹Œ, UITabBarAppearance๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜‘๊ฐ™์ด ์ ์šฉ๋œ๋‹ค.

 
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.