Post

Replies

Boosts

Views

Activity

Reply to The number on the screen does not change when the variable changes.
import SwiftUI struct S1: View { @State var aa = 0 var body: some View { TabView{ S2(aa: $aa) .tabItem { Image(systemName: "person.fill") Text("1") } .toolbarBackground(.visible, for: .tabBar) .toolbarBackground(.ultraThickMaterial, for: .tabBar) S3(aa: $aa) .tabItem { Image(systemName: "person.fill") Text("2") } .toolbarBackground(.visible, for: .tabBar) .toolbarBackground(.ultraThickMaterial, for: .tabBar) } } } struct S1_Previews: PreviewProvider { static var previews: some View { S1() } } import SwiftUI struct S2: View { @State var bb = 0 @Binding var aa: Int var body: some View { VStack{ HStack{ Button { aa += 1 if aa > bb { bb += 1 } } label: { Text("+") .frame(width: 140, height: 50) .background { RoundedRectangle(cornerRadius: 15, style: .continuous) .fill(Color(red: 0.888, green: 0.536, blue: 0.785)) .frame(width: 140, height: 50) } .foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69)) .multilineTextAlignment(.center) .bold() .dynamicTypeSize(.accessibility1) } Button { aa -= 1 if aa < bb { bb -= 1 } } label: { Text("-") .frame(width: 140, height: 50) .background { RoundedRectangle(cornerRadius: 15, style: .continuous) .fill(Color(red: 0.888, green: 0.536, blue: 0.785)) .frame(width: 140, height: 50) } .foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69)) .multilineTextAlignment(.center) .bold() .dynamicTypeSize(.accessibility1) } } Text(String(bb)) .frame(width: 140, height: 50) .background { RoundedRectangle(cornerRadius: 15, style: .continuous) .fill(Color(red: 0.888, green: 0.536, blue: 0.785)) .frame(width: 140, height: 50) } .foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69)) .multilineTextAlignment(.center) .bold() .dynamicTypeSize(.accessibility3) } .onChange(of: aa) { newValue in if aa > bb { bb += 1 } else if aa < bb { bb -= 1 } } } } struct S2_Previews: PreviewProvider { static var previews: some View { S2(aa: .constant(0)) } } import SwiftUI struct S3: View { @Binding var aa: Int var body: some View { HStack{ Button { aa += 1 } label: { Text("+") .frame(width: 140, height: 50) .background { RoundedRectangle(cornerRadius: 15, style: .continuous) .fill(Color(red: 0.888, green: 0.536, blue: 0.785)) .frame(width: 140, height: 50) } .foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69)) .multilineTextAlignment(.center) .bold() .dynamicTypeSize(.accessibility1) } Button { aa -= 1 } label: { Text("-") .frame(width: 140, height: 50) .background { RoundedRectangle(cornerRadius: 15, style: .continuous) .fill(Color(red: 0.888, green: 0.536, blue: 0.785)) .frame(width: 140, height: 50) } .foregroundColor(Color(red: 1.002, green: 0.967, blue: 0.69)) .multilineTextAlignment(.center) .bold() .dynamicTypeSize(.accessibility1) } } } } struct S3_Previews: PreviewProvider { static var previews: some View { S3(aa: .constant(0)) } }
Mar ’23
Reply to SwiftUI - Navigation Bar Fails to Switch From Scroll Edge Appearance
You might want to consider implementing a Navigation Bar Appearance ViewModifier and applying it to your VStack struct MainView: View { var body: some View { ScrollView { Text("Text") } .frame(maxWidth: .infinity) } } struct TabBarView: View { var body: some View { Color.pink .frame(maxWidth: .infinity) .frame(height: 55) } } struct ContentView: View { var body: some View { NavigationView { VStack(spacing: 0) { MainView() TabBarView() // Commenting this out fixes the problem. } .background(.red) // Commenting this out _also_ fixes the problem. .opaqueNavigationBar( foregroundColor: .white, backgroundColor: .systemBrown, tintColor: .systemIndigo, shadowColor: .darkGray) .navigationBarTitleDisplayMode(.inline) .navigationTitle("Main View") } } } public struct OpaqueNavigationBarAppearance: ViewModifier { public init( font: UIFont = .preferredFont(forTextStyle: .headline), largeTitleFont: UIFont = .preferredFont(forTextStyle: .largeTitle), foregroundColor: UIColor = .clear, backgroundColor: UIColor = .secondarySystemBackground, tintColor: UIColor = .clear, shadowColor: UIColor = .clear) { let appearance = UINavigationBarAppearance() appearance.configureWithOpaqueBackground() appearance.titleTextAttributes = [.foregroundColor: foregroundColor, .font: font] appearance.largeTitleTextAttributes = [.foregroundColor: foregroundColor, .font: largeTitleFont] appearance.shadowColor = shadowColor appearance.backgroundColor = backgroundColor UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().scrollEdgeAppearance = appearance UINavigationBar.appearance().compactAppearance = appearance UINavigationBar.appearance().tintColor = tintColor } public func body(content: Content) -> some View { return content } } public extension View { func opaqueNavigationBar( font: UIFont = .preferredFont(forTextStyle: .headline), largeTitleFont: UIFont = .preferredFont(forTextStyle: .largeTitle), foregroundColor: UIColor = .clear, backgroundColor: UIColor = .secondarySystemBackground, tintColor: UIColor = .clear, shadowColor: UIColor = .clear) -> some View { ModifiedContent( content: self, modifier: OpaqueNavigationBarAppearance( font: font, largeTitleFont: largeTitleFont, foregroundColor: foregroundColor, backgroundColor: backgroundColor, tintColor: tintColor, shadowColor: shadowColor)) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
May ’23
Reply to Widget background Image not loading IOS 17 Xcode
Here's a sample implementation of containerBackground. LMEntryView is your widget view. struct LMWidget: Widget { var supportedFamilies: [WidgetFamily] { [.systemSmall, .systemMedium] } var body: some WidgetConfiguration { StaticConfiguration(kind: "LMWidget", provider: LMTimelineProvider()) { entry in if #available(iOS 17, *) { LMEntryView(entry: entry) .frame(maxWidth: .infinity, maxHeight: .infinity) .containerBackground(Color("WidgetBackground"), for: .widget) } else { LMEntryView(entry: entry) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color("WidgetBackground")) } } .configurationDisplayName("L_M_Widget_Title".localized) .description("L_M_Widget_Description".localized) .supportedFamilies(supportedFamilies) .contentMarginsDisabled() } }
Jan ’24
Reply to iOS26 bottom toolbars space broken
.toolbar { ToolbarItemGroup(placement: .bottomBar) { GlassEffectContainer { Button { // } label: { VStack { Image( systemName: "car" ) Text("Car") .font(.caption2) } .padding() } .tint(.red) Button { // } label: { VStack { Image( systemName: "person" ) Text("Person") .font(.caption2) } .padding() } .tint(.blue) Button { } label: { VStack { Image( systemName: "house" ) Text("House") .font(.caption2) } .padding() } .tint(.green) Button { } label: { VStack { Image( systemName: "lightbulb" ) Text("Lightbulb") .font(.caption2) } .padding() } .tint(.indigo) Button { } label: { VStack { Image( systemName: "trash" ) Text("Trash") .font(.caption2) } .padding() } .tint(.purple) } .frame(alignment: .leadingLastTextBaseline) .frame(height: 64) .frame(maxWidth: .infinity) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’25
Reply to Setting Tint Color for Prominent Style UIBarButtonItems at App Level in iOS26
let image1 = UIImage( systemName: "person" ) let barButtonItem1 = UIBarButtonItem.init( image: image1, style: .plain, target: nil, action: nil ) barButtonItem1.tintColor = .systemPink let image2 = UIImage( systemName: "book" ) let barButtonItem2 = UIBarButtonItem.init( image: image2, style: .plain, target: nil, action: nil ) barButtonItem2.tintColor = .systemMint navigationItem.rightBarButtonItems = [ barButtonItem1, flexibleSpace, barButtonItem2 ]
Topic: UI Frameworks SubTopic: UIKit Tags:
Aug ’25