UIButton & Configuration

o2k_iOS·2025년 3월 25일

기존 UIButton 방식의 한계

기존 UIButton 구성 방식에서는 버튼 객체를 생성한 후 setTitlesetImage 등의 메서드를 통해 버튼의 모양을 구성해야 했습니다1. 이 방식은 코드가 길어지고 직관적이지 않았으며, 다양한 상태에 따른 스타일링이 복잡했습니다.

private let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
button.setImage(UIImage(systemName: "heart"), for: .normal)`

iOS 15에서 도입된 Configuration의 장점

  1. 일관된 스타일링: 다양한 버튼 스타일(plain, filled, gray, tinted 등)을 쉽게 적용할 수 있습니다
  2. 코드 간소화: 여러 속성을 하나의 Configuration 객체로 관리할 수 있어 코드가 간결해집니다
  3. 재사용성 향상: 동일한 Configuration을 여러 버튼에 적용할 수 있어 일관된 UI를 쉽게 구현할 수 있습니다
  4. 동적 업데이트 지원: ConfigurationUpdateHandler를 통해 버튼 상태에 따른 스타일 변경을 더 직관적으로 구현할 수 있습니다
  5. 서브타이틀 지원: 기존 방식에서는 없었던 서브타이틀 기능이 추가되었습니다

구현 방식의 차이

기존 방식:

let button = UIButton()
button.setTitle("Normal", for: .normal)
button.setTitle("Highlighted", for: .highlighted)
button.backgroundColor = .black

Configuration 방식:

var config = UIButton.Configuration.filled()
config.title = "Title"
config.subtitle = "Subtitle"
config.image = UIImage(systemName: "swift")
config.baseBackgroundColor = .darkGray
let button = UIButton(configuration: config)
profile
느려도 조금씩 성장

0개의 댓글