๋ฐ์ํ
์ค๋ ๋ง๋ 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)
}
}
๋ฐ์ํ