Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's iOS Story/๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฌ๋“ค

[UIKit] UIButton Configuration ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์ž (feat. custom button ๋งŒ๋“ค๊ธฐ)

๊ฐ์ž ๐Ÿฅ” 2022. 11. 4. 03:11
๋ฐ˜์‘ํ˜•



์˜ค๋Š˜ ๋งŒ๋‚œ UIButton~~ 

์šฐ๋ฆฌ ํƒ€๊ฒŸ์ด 14, 15์˜€๋‚˜
์ง€๊ธˆ๋ถ€ํ„ฐ ๊ทธ๋Ÿผ ๋Œ€์‘์„ ํ•ด์ค˜์•ผํ•˜๋Š”๊ฐ€?
#available ์œผ๋กœ → ๋‹ต๋ณ€์€ ใ…‡ใ…‡!!!

UIButton ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•ด์ฃผ์„ธ์š”~

์šฐ๋ฆฌ์˜ ์•ฑ ํƒ€๊ฒŸ์€ iOS 14.0 ์ด์ƒ์ด๋‹ค.

๊ทธ๋ƒฅ ์ผ๋ฐ˜ UIButton์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋„ ๋˜๊ฒ ๊ฑฐ๋‹ˆ~ ์‹ถ์–ด์„œ ๊ทธ๋ƒฅ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ, ์ด๋Ÿฐ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์•˜๋‹ค.

์•„ํ•˜!!! ์ผ๋ฐ˜ ๋ฒ„ํŠผ์—์„œ ์ž‘์„ฑํ•˜๋˜ ์ฝ”๋“œ๋“ค์ด iOS 15.0 ์—์„œ๋Š” ์•„์˜ˆ ์ž‘๋™์„ ์•ˆํ•  ์ˆ˜ ๋„ ์žˆ๊ฒ ๊ตฌ๋‚˜!! ๊ทธ๋Ÿผ ๋ฌด์กฐ๊ฑด ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ์ง€..์•”…๊ทธ๋ž˜ ใ… ใ…  (์ž๋™์œผ๋กœ ์ข€ ํ•ด์ฃผ์ง€)

์ƒ์„ฑ๋˜๋Š” ๋ฒ„ํŠผ์—๋‹ค๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

private func configureUI() {
        if #available(iOS 15.0, *) {
            var buttonConfig = UIButton.Configuration.filled()
            buttonConfig.titleAlignment = .leading
            buttonConfig.baseBackgroundColor = .evyBlack1
            buttonConfig.cornerStyle = .capsule
            buttonConfig.preferredSymbolConfigurationForImage = Size.buttonImageSize
            buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 10, bottom: 8, trailing: 30)
            configuration = buttonConfig
            print("์—ฌ๊ธธ์ง€๋‚˜๊ธดํ•ด?")
        } else {
            self.invalidateIntrinsicContentSize()
            self.titleLabel?.textAlignment = .center
            self.backgroundColor = .evyBlack1
            self.imageView?.contentMode = .scaleAspectFit
            self.constraint(.heightAnchor, constant: Size.buttonHeigth)
            self.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 30)
        }
    }
  • #available(iOS 15.0, *): ios 15๋ฒ„์ „ ์ด์ƒ์ผ ๊ฒฝ์šฐ์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ํ†ต๊ณผํ•˜๊ฒŒ ๋œ๋‹ค.
  • UIButton.Configuration(): ๋ฒ„ํŠผ๊ณผ ๊ทธ ๋‚ด์šฉ์˜ ๋ชจ์–‘๊ณผ ๋™์ž‘์„ ์ง€์ •ํ•˜๋Š” ๊ตฌ์„ฑ
  • ํ•ด๋‹น configuration๋ฅผ ์ €์žฅํ•ด๋†“์€ buttonConfig ๊ฐ์ฒด์—๋‹ค๊ฐ€ ๋‹ค์–‘ํ•œ ์Šคํƒ€์ผ์„ ์ฃผ๊ณ , ๋งˆ์ง€๋ง‰์— configuration์— ์ง€์ •ํ•ด์ฃผ๋ฉด ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋ฒ„ํŠผ ์Šคํƒ€์ผ์ด configuration์— ์ •์˜๋œ๋‹ค.

 

๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ๋กœ ๋“ค์–ด๊ฐ€๋Š” UIButton์˜ ์ตœ์ข… ์ฝ”๋“œ

//
//  GotoSomewhereButton.swift
//  EarthValley80
//
//  Created by LeeJiSoo on 2022/11/02.
//

import UIKit

final class GotoSomewhereButton: UIButton {

    private enum Size {
        static let buttonContentEdgeIneset: CGFloat = -10.0
        static let buttonFontSize: CGFloat = 16.0
        static let buttonHeigth: CGFloat = 50.0
        static let buttonCornerRadius: CGFloat = 25.0
        static let buttonImageSize: UIImage.SymbolConfiguration = UIImage.SymbolConfiguration(pointSize: 35)
    }

    // MARK: - init

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configureUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - func

    private func configureUI() {
        if #available(iOS 15.0, *) {
            var buttonConfig = UIButton.Configuration.filled()
            var buttonTitleAttribute = AttributedString()
            buttonTitleAttribute.font = .font(.medium, ofSize: 16)
            buttonConfig.attributedTitle = buttonTitleAttribute
            buttonConfig.titleAlignment = .leading
            buttonConfig.baseBackgroundColor = .evyBlack1
            buttonConfig.cornerStyle = .capsule
            buttonConfig.preferredSymbolConfigurationForImage = Size.buttonImageSize
            buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 10, bottom: 7, trailing: 30)
            configuration = buttonConfig
        } else {
            self.invalidateIntrinsicContentSize()
            self.titleLabel?.textAlignment = .center
            self.titleLabel?.setLineSpacing(kernValue: -0.32, lineHeightMultiple: 0.83)
            self.backgroundColor = .evyBlack1
            self.imageView?.contentMode = .scaleAspectFit
            self.constraint(.heightAnchor, constant: Size.buttonHeigth)
            self.contentEdgeInsets = UIEdgeInsets(top: 8, left: 10, bottom: 7, right: 30)
            self.layer.cornerRadius = Size.buttonCornerRadius
        }
    }

    func changeButtonContents(buttonImage: UIImage, buttonTitle: String) {
        self.setImage(buttonImage, for: .normal)
        self.setTitle(buttonTitle, for: .normal)
    }
}
๋ฐ˜์‘ํ˜•