Swift
Swift - UIButton image Scale 문제
Goniii
2024. 10. 15. 18:52
문제 상황
- Saved Button의 이미지가 꽉 차지 않는 문제
// 저장 버튼
public lazy var btnSaved = UIButton().then { btn in
var config = UIButton.Configuration.plain()
config.image = UIImage(systemName: "bookmark.fill")
config.image?.withTintColor(.black)
btn.configuration = config
btn.imageView?.contentMode = .scaleAspectFit
btn.translatesAutoresizingMaskIntoConstraints = false
}
해결 과정
- UIButton.clipsToBounds : 하위 뷰가 뷰의 경계 내에 제한하는지 결정하는 bool 값
- true일 때 서브 뷰들은 뷰의 테두리 기준으로 잘림
- clipsToBounds, contentInsets 설정
// 저장 버튼
public lazy var btnSaved = UIButton().then { btn in
var config = UIButton.Configuration.plain()
config.image = UIImage(systemName: "bookmark.fill")
config.image?.withTintColor(.black)
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 5, trailing: 0)
btn.configuration = config
btn.imageView?.contentMode = .scaleAspectFit
btn.clipsToBounds = true
btn.translatesAutoresizingMaskIntoConstraints = false
}
→ contentInsets을 설정하지 않으면 왼쪽과 같이 이미지가 잘림
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 5, trailing: 0)
→ 버튼 내부 이미지의 위치를 바텀에서 5만큼 띄어주므로써 이미지가 잘리지 않음
참고 자료
728x90