Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Protocol
- ribs
- 스위프트
- 클린 코드
- uitableview
- 애니메이션
- SWIFT
- clean architecture
- 리펙토링
- Refactoring
- uiscrollview
- combine
- UITextView
- rxswift
- HIG
- swift documentation
- MVVM
- map
- collectionview
- UICollectionView
- ios
- RxCocoa
- swiftUI
- Clean Code
- Observable
- scrollview
- Xcode
- tableView
- Human interface guide
- 리팩토링
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[iOS - SwiftUI] background vs overlay 차이 본문

SwiftUI의 bacgrkound와 overlay
- background와 overlay 옵션 모두 특정 뷰에 겹치게 뷰를 배치할 때 사용하는 메소드
- 차이점은 background은 뷰 뒤에, overlay는 뷰 앞쪽에 배치 중첩됨
ex) background를 사용하면 뷰 뒷쪽에 배치됨
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.background {
Color.blue
}
}
.padding()
}
}

ex) overay를 사용하면 뷰 앞쪽에 배치되므로 뷰가 가려짐
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.overlay(alignment: .center) {
Color.blue
}
}
.padding()
}
}

주의할 점
- background를 사용하면 뷰 뒷쪽에 배치되어 안보여야 하는 것이 아닌가?
- 사실상 background는 배경 색상을 정하는 것처럼 앞 쪽 Text인 "Hello, world!" 콘텐츠를 제외하고 alpha 값을 0으로 만들어 투명상태로 만들어주는 것
ex) background {} 클로저는 background()메소드와 동일한 역할을 수행함
- 단순히 background()는 배경 색상을 입힐때 사용하고, background {}는 배경 색상 이외의 값도 표출해줄 때 사용
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.background(Color.green)
}
.padding()
}
}

ex) background {}는 배경 색상 이외의 값도 표출해줄 때 사용
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.background {
Circle()
.frame(width: 30, height: 30)
.opacity(0.1)
}
}
.padding()
}
}

'iOS 응용 (SwiftUI)' 카테고리의 다른 글
Comments