์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- realm
- XCTest
- TCA
- Lv2
- rxcocoa
- RxSwift
- designpattern
- ๋ฐฑ์ค
- combine
- BOJ
- Kuring
- tableView
- Xcode
- reactorkit
- UIKit
- arkit
- CollectionView
- SwiftUI
- BFS
- ํ๋ก๊ทธ๋๋จธ์ค
- Swfit
- SnapKit
- ios
- visionOS
- swift
- ํจ์คํธ์บ ํผ์ค
- raywenderlich
- MVVM
- node.js
- Flutter
Archives
- Today
- Total
lgvv98
[SwiftUI] menu (feat. Picker) ๋ณธ๋ฌธ
menu (feat. Picker)
โ ๋ฉ๋ด์ ๋ํด์ ์์๋ณด์.
์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ ์ฌ์ฉ์ํ๋๋ฐ, ์ด๋๋ ๋ณด๋๊น menu๋ฅผ ์ฌ์ฉํ ์ ์๋ ๋ฒํผ์ด ๋ค์ด์๋ค.
โ ์ฝ๋
import SwiftUI
let myPets = ["๋ฉ๋ฉ์ด ๐ถ", "์ผ์น์ด ๐ฏ", "์ฐ์ฐ์ด ๐น"]
struct ContentView: View {
@State private var shouldShowAlert : Bool = false // alert๋ฅผ ๋์ฐ๊ธฐ ์ํจ.
@State private var myText : String = "" // ํ
์คํธ
@State private var selected : Int = 0 // ์ ํํ ์ธ๋ฑ์ค
var body: some View {
NavigationView {
// Spacer()๋ ์๋จ 1 ํ๋จ 2์ ๋น์จ๋ก
VStack(spacing: 20) {
Spacer()
Text("\(myPets[selected])") // ๋ทฐ์์ ๋ํ๋๋ ๊ธ์
.font(.system(size: 60))
.bold()
Text("์ฐ์ธก ์๋จ์ ๋ก๋ก๋ก์ ๋๋ฌ์ฃผ์ธ์!")
.font(.title2)
.fontWeight(.black)
Spacer()
Spacer()
}
.padding()
// ๋ค๋น๊ฒ์ด์
ํ์ดํ -> swiftUI 3.0์์๋ใ
toolbar์์ placment: .principal๋ก ์ฌ์ฉ
.navigationTitle("ํ์ด์")
.toolbar(content: {
ToolbarItem(placement: .navigationBarTrailing, content: {
Menu { // ๋ฉ๋ด๋ก ๋ฌถ๊ธฐ ์ํด์!
Button(action: {
// ์ก์
print("์ค๋๋ ๋นก์ฝ๋ฉ ๋ฒํผํด๋ฆญ!")
shouldShowAlert = true
myText = "์ค๋๋ ๋นก์ฝ๋ฉ!"
}, label: {
// ๋ผ๋ฒจ
Label("์ค๋๋ ๋นก์ฝ๋ฉ!" , systemImage: "flame.fill")
})
// ๋ฉํฐ ํธ๋ ์ผ๋ง ํด๋ก์
Button {
print("์ง์์ ์ฌ๋ ๋ ๋ฒํผํด๋ฆญ!")
shouldShowAlert = true
myText = "์ง์์ ์ฌ๋ ๋ !"
} label : {
Label("์ง์์ ์ฌ๋ ๋ !" , systemImage: "house.fill")
}
Section { // ์น์
์ผ๋ก ๋ฌถ์ด์ ์ฒ๋ฆฌํ๊ธฐ ์ํด์
// ๋ฉํฐ ํธ๋ ์ผ๋ง ํด๋ก์
Button {
} label : {
Label("์ ํ์ผ ๋ง๋ค๊ธฐ" , systemImage: "doc")
}
Button {
} label : {
Label("์ ํด๋ ๋ง๋ค๊ธฐ" , systemImage: "folder")
}
}
Section {
// ๋ฉํฐ ํธ๋ ์ผ๋ง ํด๋ก์
Button {
// action
} label : {
Label("ํ์ผ ๋ชจ๋ ์ญ์ " , systemImage: "trash")
}
}
// Picker์๋ Bindingํด์ผ ํ๋ค.
Picker(selection: $selected, label: Text("์ ์๋๋ฌผ ์ ํ")) {
// indices๋ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์
ForEach(myPets.indices, id: \.self, content: { index in
Text("\(myPets[index])").tag(index)
})
}
} label: {
// ์ด ๋ถ๋ถ์์ label๋ก ๋ฒํผ ๋ง๋ ๋ค!
Circle().foregroundColor(Color.init(#colorLiteral(red: 0.8349713402, green: 0.1496438846, blue: 0.7993865004, alpha: 1)))
.frame(width: 50, height: 50)
.overlay(
Label("๋๋ณด๊ธฐ", systemImage: "ellipsis")
.font(.system(size: 30))
.foregroundColor(.white)
)
} // ๋ฉ๋ด
})
}) // toolbar
.alert(isPresented: $shouldShowAlert, content: { // alert ํตํด์ ์๋ฆผ ๋ณด์ฌ์ค
Alert(
title: Text("์๋ฆผ"),
message: Text("\(myText)"),
dismissButton: .default(Text("ํ์ธ"))
)
})
}
}
}
'apple > ๐ SwiftUI & Combine' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SwiftUI] ViewModifier (0) | 2022.06.02 |
---|---|
[SwiftUI] lazyVGrid (0) | 2022.05.25 |
[SwiftUI] Picker, segmentedStyle (feat. enum CaseIterable) (0) | 2022.05.23 |
[SwiftUI] Toast, popup (0) | 2022.05.23 |
[SwiftUI] TextField, SecureField (0) | 2022.05.23 |
Comments