본문 바로가기

학습 노트

(191)
16. Button & Link & Menu Button Apple Developer Documentation developer.apple.com 일반적으로 사용하는 파라미터는 아래와 같다 action Button의 동작 label Button의 UI title Button의 이름 role Alert, Context에 Button을 사용하는 경우 특정 역할을 부여 struct ControlsView: View { @State private var text = "" var body: some View { Text(text) Button("test", role: .destructive) { text = "Hello" } } } Link Apple Developer Documentation developer.apple.com struct Controls..
TextField 입력값 제한하기 입력값을 제한할 때는 보통 세 가지를 고려해야 한다. 숫자만 입력한다고 가정 해 보자. 사용자는 숫자키보드(numeric)가 아닌 SW키보드를 사용할 가능성이 있다. 블루투스나 iPad의 스마트 키보드등의 외장 HW키보드를 사용할 가능성이 있다. 복사, 붙여넣기로 값을 입력할 수 있다. struct LabelView: View { @State private var value = "" @State private var input = "" let formatter: NumberFormatter = { let formatter = NumberFormatter() formatter.numberStyle = .decimal return formatter }() var body: some View { Form { ..
14 ~ 15. TextField & TextField Style & TextEditor TextField Apple Developer Documentation developer.apple.com TextField는 한 줄 입력에 최적화된 View이다. struct LabelView: View { @State private var value = "" var body: some View { Form { Text("testing") .padding() TextField("title", text: $value, prompt: Text("prompt1")) .padding() TextField("title", text: $value) .padding() } } } 생성자의 기본 파라미터는 세 개로 아래와 같다. title iOS와 iPadOS에서 placeholder로 사용된다. macOS에서 La..
13. Label & Font Label Apple Developer Documentation developer.apple.com Label은 이미지와 문자열을 표시하기에 적합한 View다. SwiftUI 초기에는 존재하지 않았기 때문에 하위 호환성을 고려해 사용해야 한다. struct LabelView: View { var body: some View { Label("Bulb", systemImage: "lightbulb") } } 기본 형태는 title + systemImage이고, Custom은 title + iCon으로 이 경우엔 Image View와 Text View를 조합해 사용하는 것과 크게 다르지 않다. struct LabelView: View { var body: some View { NavigationView { B..
12. Text Text 문자열을 표시하는 가장 기초적인 View이지만 기능이 꽤나 많은 편에 속한다. Locailzed String Localizable 파일 생성하기 프로젝트 내에 'Strings File'을 생성하고, 이름을 Localizable로 변경한다. 다른 이름을 사용하지 않는 것이 좋다. Localizable 파일 적용하기 프로젝트의 설정에서 Localizations를 찾아 원하는 언어를 추가한다. 이때 우측 상단의 검색을 이용하면 편리하다. Base와 English가 기본으로 적용돼 있다. 앞서 생성한 Localizable.strings 파일을 선택한 뒤 File Inspector에서 'Localize...' 버튼을 누른다. 아무 언어나 선택해 Localize를 누르면 이전에 눌었던 Localize.....
10 ~ 11. Sheet / FullScreenCover & Popover Sheet / FullScreenCover Sheet는 화면을 Modal 방식으로 표시한다. Modal은 화면 전체를 채우는 Full Screen Modal과 화면의 일부를 채우는 Sheet, Card, Card Modal 방식이 있다. Sheet는 화면에 표시될 때 아래에서 위로 올라오는 Present 방식으로 표시되고, 사라질 때 위에서 아래로 사라지는 Dismiss 방식으로 사라진다. struct Nav_Sheet: View { @State private var statusMessage = "" @State private var sheetState = false var body: some View { VStack { Text(statusMessage) .font(.largeTitle) Button(..
08 ~09. Alert & Confirmation Dialog Alert SwiftUI로 Alert를 표시하는 데에는 준비물이 하나 필요하다. struct View_Alert: View { @State private var result = "" @State private var alertStat = false var body: some View { VStack { Text(result) .font(.largeTitle) Button(action: { }, label: { Text("Show Alert") }) .padding() } } } 바로 State Variable로 위의 코드에서는 alertStat이라는 이름을 가진다. alert은 해당 변수를 조작하여 자신의 상태를 변경하는데 사용하기 때문에 반드시 필요하다. struct View_Alert: View { ..
07. Overlay Overlay View 위에 View를 덮는 View다. struct View_Group: View { var body: some View { ScrollView(.vertical, showsIndicators: false) { VStack { RoundedRectangle(cornerRadius: 15) .frame(width: 300, height: 300) .foregroundColor(.yellow) .overlay { RoundedRectangle(cornerRadius: 15) .frame(width: 100, height: 100) .foregroundColor(.red) } } ZStack { RoundedRectangle(cornerRadius: 15) .frame(width: 300, ..
05 ~ 06. Scroll View & Form Scroll View struct View_Group: View { var body: some View { VStack { Group { Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") } .font(.largeTitle.bold()) .foregroundColor(.red) Group { Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") Text("Hello, World!"..
03 ~ 04. Spacer & Group Spacer struct View_Spacer: View { var body: some View { VStack(spacing: 0) { HStack { Image(systemName: "suit.heart.fill") .resizable() .frame(width: 70, height: 70) .foregroundColor(.white) } .padding() .background(Color.blue) Spacer() VStack { Image(systemName: "suit.spade.fill") .resizable() .frame(width: 70, height: 70) .foregroundColor(.white) } .padding() .background(Color.red) } } } Space..