Post

Replies

Boosts

Views

Activity

Reply to TextField Binding
Well, I guess I could do the following. import SwiftUI struct ContentView: View { @State private var names: [String] = ["Jim Thorton", "Susan Murphy", "Tom O'Donnell", "Nancy Smith"] var body: some View { HStack { ForEach($names, id: \.self) { $name in TextField("", text: $name) .fixedSize() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Deleting a View Instance with the Tap of a Button
I got it thanks to Claude31. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) ForEach(monster.items) { item in KeywordRow(id: item.id).environmentObject(monster) } } } } struct Keyword: Identifiable { let id = UUID() } struct KeywordRow: View { @State var id = UUID() @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { Text("ID: \(id)") Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to Getting an Updated Value Through @EnvironmentObject
import SwiftUI struct ContentView: View { @EnvironmentObject var gameSettings: GameSettings var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { RightView().environmentObject(gameSettings) .frame(width: geo.size.width / 2.0, height: geo.size.height) Spacer() } VStack { HStack { Spacer() Button { print("\(gameSettings.score)") } label: { Text("Print") .font(.largeTitle) }.padding(.trailing, 40.0) } Spacer() } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’22
Reply to Passing a Variable Between Two Unrelated Views Without Binding
I guess I've solved a problem on my own again. import SwiftUI struct ContentView: View { @StateObject var monster = MonsterObservable.shared var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { RightView() .frame(width: geo.size.width, height: geo.size.height) } ShowDialogView(isShowing: $monster.showDialog) { } .frame(width: 500, height: 600, alignment: .center) .cornerRadius(10.0) } } } } struct ShowDialogView<Content: View>: View { @Binding var isShowing: Bool @ViewBuilder let content: () -> Content var body: some View { Group { if isShowing { ZStack { Color.brown VStack { Spacer() Button { isShowing = false } label: { Text("Close me") .font(.largeTitle) }.padding([.top, .bottom], 100.0) } } } } } } class MonsterObservable: ObservableObject { static let shared = MonsterObservable() @Published var showDialog = false } class GameScore: ObservableObject { static let shared = GameScore() @Published var score = 0 } struct RightView: View { @ObservedObject var monster = MonsterObservable.shared var body: some View { ZStack { Color.red Button { monster.showDialog = true } label: { Text("Change") .font(.largeTitle) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’22
Reply to Telling a View to show a Dialog from Another
I guess I've solved my question as follows. import SwiftUI struct ContentView: View { @ObservedObject var monster: MonsterObservable var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { LeftView() .frame(width: geo.size.width / 2.0, height: geo.size.height, alignment: .leading) RightView(showMe: $monster.showDialog) .frame(width: geo.size.width / 2.0, height: geo.size.height, alignment: .trailing) } ShowDialogView(isShowing: monster.showDialog) { } .frame(width: 500, height: 600, alignment: .center) .cornerRadius(10.0) } } } } class MonsterObservable: ObservableObject { @Published var showDialog = false } import SwiftUI struct RightView: View { @Binding var showMe: Bool var body: some View { ZStack { Color.red Button { showMe = true } label: { Text("Tap me") .font(.largeTitle) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’22
Reply to 'animation' was deprecated in iOS 15.0
I guess I've somehow worked it out as follows. import SwiftUI struct ContentView5: View { @State private var larger = true var body: some View { VStack { Circle() .fill(Color.pink) .frame(width: 150, height: 150) .scaleEffect(larger ? 2 : 1) .animation(.easeInOut(duration: 1).repeatForever(), value: larger) }.onAppear { larger = false } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’22
Reply to Where and How to Create FileManager as a Singleton?
I've figured it out. You use UIApplicationDelegateAdaptor for iOS, NSApplicationDelegateAdaptor for Cocoa. import SwiftUI @main struct MyCrazyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } class AppDelegate: UIResponder, UIApplicationDelegate { let fileManager = FileManager.default } In this fashion, you will access to fileManager in AppDelegate inside another View.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’22
Reply to Picker with Unexpected Results
I've solved both of the issues with the following. struct PickColorView: View { @State var numberIndex: Int = 4 @State var textSizeIndex: Int = 0 let numbers: [Int] = [14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60] var body: some View { NavigationView { Form { Picker(selection: $numberIndex, label: Text("Numbers")) { ForEach((0...20), id: \.self) { Text("\($0)") } }.onChange(of: numberIndex) { newIndex in print("Index: \(newIndex)") } Picker("Numbers", selection: $textSizeIndex) { ForEach(0..<numbers.endIndex) { index in let number = numbers[index] Text("\(number)") } }.onChange(of: textSizeIndex) { newIndex in print("Index: \(newIndex)") } } .navigationBarTitle("Settings") .navigationBarHidden(false) } .navigationViewStyle(StackNavigationViewStyle()) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Horizontally-Aligned TextField Wrap?
The following Stack Overflow could be a solution that I've been looking for. https://stackoverflow.com/questions/58842453/swiftui-hstack-with-wrap
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to TextField Binding
Well, I guess I could do the following. import SwiftUI struct ContentView: View { @State private var names: [String] = ["Jim Thorton", "Susan Murphy", "Tom O'Donnell", "Nancy Smith"] var body: some View { HStack { ForEach($names, id: \.self) { $name in TextField("", text: $name) .fixedSize() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Deleting a View Instance with the Tap of a Button
I got it thanks to Claude31. import SwiftUI struct ContentView: View { @ObservedObject var monster: Monster var body: some View { VStack { Button { monster.items.append(Keyword()) } label: { Text("Add me!") }.padding(.vertical, 10.0) ForEach(monster.items) { item in KeywordRow(id: item.id).environmentObject(monster) } } } } struct Keyword: Identifiable { let id = UUID() } struct KeywordRow: View { @State var id = UUID() @EnvironmentObject var monster: Monster var body: some View { VStack { HStack { Text("ID: \(id)") Button { monster.items.removeAll(where: { $0.id == id} ) } label: { Text("Delete") } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Showing a Constructor Dialog View with an ObservedObject Object
I guess the user State object in ContentView comes with default values.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to Getting an Updated Value Through @EnvironmentObject
import SwiftUI struct ContentView: View { @EnvironmentObject var gameSettings: GameSettings var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { RightView().environmentObject(gameSettings) .frame(width: geo.size.width / 2.0, height: geo.size.height) Spacer() } VStack { HStack { Spacer() Button { print("\(gameSettings.score)") } label: { Text("Print") .font(.largeTitle) }.padding(.trailing, 40.0) } Spacer() } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to Passing a Variable Between Two Unrelated Views Without Binding
I guess I've solved a problem on my own again. import SwiftUI struct ContentView: View { @StateObject var monster = MonsterObservable.shared var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { RightView() .frame(width: geo.size.width, height: geo.size.height) } ShowDialogView(isShowing: $monster.showDialog) { } .frame(width: 500, height: 600, alignment: .center) .cornerRadius(10.0) } } } } struct ShowDialogView<Content: View>: View { @Binding var isShowing: Bool @ViewBuilder let content: () -> Content var body: some View { Group { if isShowing { ZStack { Color.brown VStack { Spacer() Button { isShowing = false } label: { Text("Close me") .font(.largeTitle) }.padding([.top, .bottom], 100.0) } } } } } } class MonsterObservable: ObservableObject { static let shared = MonsterObservable() @Published var showDialog = false } class GameScore: ObservableObject { static let shared = GameScore() @Published var score = 0 } struct RightView: View { @ObservedObject var monster = MonsterObservable.shared var body: some View { ZStack { Color.red Button { monster.showDialog = true } label: { Text("Change") .font(.largeTitle) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to Telling a View to show a Dialog from Another
I guess I've solved my question as follows. import SwiftUI struct ContentView: View { @ObservedObject var monster: MonsterObservable var body: some View { GeometryReader { geo in ZStack { HStack(spacing: 0.0) { LeftView() .frame(width: geo.size.width / 2.0, height: geo.size.height, alignment: .leading) RightView(showMe: $monster.showDialog) .frame(width: geo.size.width / 2.0, height: geo.size.height, alignment: .trailing) } ShowDialogView(isShowing: monster.showDialog) { } .frame(width: 500, height: 600, alignment: .center) .cornerRadius(10.0) } } } } class MonsterObservable: ObservableObject { @Published var showDialog = false } import SwiftUI struct RightView: View { @Binding var showMe: Bool var body: some View { ZStack { Color.red Button { showMe = true } label: { Text("Tap me") .font(.largeTitle) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to Telling a View to show a Dialog from Another
It's trivial as follows. import SwiftUI struct LeftView: View { var body: some View { ZStack { Color.green } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’22
Reply to 'animation' was deprecated in iOS 15.0
I guess I've somehow worked it out as follows. import SwiftUI struct ContentView5: View { @State private var larger = true var body: some View { VStack { Circle() .fill(Color.pink) .frame(width: 150, height: 150) .scaleEffect(larger ? 2 : 1) .animation(.easeInOut(duration: 1).repeatForever(), value: larger) }.onAppear { larger = false } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to Where and How to Create FileManager as a Singleton?
I've figured it out. You use UIApplicationDelegateAdaptor for iOS, NSApplicationDelegateAdaptor for Cocoa. import SwiftUI @main struct MyCrazyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } class AppDelegate: UIResponder, UIApplicationDelegate { let fileManager = FileManager.default } In this fashion, you will access to fileManager in AppDelegate inside another View.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to How to move objects to the right side?
HStack + Spacer() should work unless you have NavigationLink inside the list.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to UIStepper in Catalyst?
The availability of UIStepper in Mac Catalyst depends on the deployment target. According to the doc, it requires iOS 13.0 or higher.
Topic: App & System Services SubTopic: Hardware Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to Syntax-highlighting a String with Multiple Keys
I've ended up creating a UIViewRepresentable struct so that I can set an NSAttributedString object to a UILabel object.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’22
Reply to Picker with Unexpected Results
I've solved both of the issues with the following. struct PickColorView: View { @State var numberIndex: Int = 4 @State var textSizeIndex: Int = 0 let numbers: [Int] = [14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60] var body: some View { NavigationView { Form { Picker(selection: $numberIndex, label: Text("Numbers")) { ForEach((0...20), id: \.self) { Text("\($0)") } }.onChange(of: numberIndex) { newIndex in print("Index: \(newIndex)") } Picker("Numbers", selection: $textSizeIndex) { ForEach(0..<numbers.endIndex) { index in let number = numbers[index] Text("\(number)") } }.onChange(of: textSizeIndex) { newIndex in print("Index: \(newIndex)") } } .navigationBarTitle("Settings") .navigationBarHidden(false) } .navigationViewStyle(StackNavigationViewStyle()) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to Any way to prevent macOS from repositioning my window?
You can do it if you hide the title bar.
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jan ’22