iOS

[iOS] 버전별 UIButton의 차이

스벅 보안관 2024. 5. 15. 20:42

안녕하세요. 스벅 보안관입니다.

오늘은 UIButton에 대해 알아보겠습니다.

 

iOS 15.0 이전

button.setImage(UIImage(named: "image"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 1, left: 2, bottom: 3, right: 12)

 

15.0 이전에는 위와 같은 코드의 형식으로 UIButton안에 Image를 설정했습니다.

하지만 이 방식은 iOS 15.0 이후부터는 지원하지 않습니다.

 

iOS 15.0 이후

출처: WWDC 2021

iOS 15.0 이후에는 UIButton의 padding과 insets를 조절할 수 있는 3가지 옵션이 생겼습니다.

  • titlePadding : 타이틀과 서브타이틀 간격
  • imagePadding : 타이틀과 이미지와의 간격
  • contentInsets : 버튼의 마진

 

이를 활용하기 위해서는 Configuration 객체를 선언해야합니다.

예시는 다음과 같습니다.

var config = UIButton.Configuration.plain()
config.title = "버튼 타이틀"
config.subtitle = "버튼 서브타이틀"

let button = UIButton(configuration: config)

 

 

UIButton Configuration의 종류

  1. Filled:
    • Filled 스타일은 버튼의 배경색이 채워져 있는 형태로, 버튼이 강조되고 명확하게 표시되도록 합니다.
    • 버튼이 전체적으로 채워진 상태이며, 텍스트는 일반적으로 배경색과 대비되는 색상으로 설정됩니다.
  2. Tinted:
    • Tinted 스타일은 버튼의 배경색이 반투명하게 처리되어 배경과 조화롭게 어우러지는 형태입니다.
    • 버튼의 배경색이 연하게 처리되며, 테두리나 텍스트가 강조됩니다.
  3. Gray:
    • Gray 스타일은 버튼이 회색으로 설정되어, 다른 스타일보다 덜 강조되는 형태입니다.
    • 버튼의 배경이 회색이며, 텍스트도 보통 중립적인 색상으로 설정됩니다.
  4. Plain:
    • Plain 스타일은 버튼의 배경이 투명하게 처리되어, 텍스트나 아이콘만 표시되는 형태입니다.
    • 버튼의 배경이 투명하며, 텍스트나 아이콘만 표시됩니다.
// Plain 스타일의 버튼 생성
let plainButton = UIButton(type: .system)
plainButton.setTitle("Plain Button", for: .normal)
plainButton.setTitleColor(.systemBlue, for: .normal)
plainButton.titleLabel?.font = UIFont.systemFont(ofSize: 18)
plainButton.translatesAutoresizingMaskIntoConstraints = false

 

 

 

오늘의 결론

UIButton이 생각보다 알아야 할게 좀..... 많이 많다...!!

'iOS' 카테고리의 다른 글

[iOS] TableViewController - [2]  (0) 2024.05.28
[iOS] TableViewController - [1]  (0) 2024.05.24
[iOS] Guard 구문을 사용하는 이유  (0) 2024.05.22
[iOS] GestureRecognizer  (0) 2024.05.20
[iOS] iOS개발의 기본적인 배경과 흐름(?)  (0) 2024.05.15