Post

Replies

Boosts

Views

Activity

Buttons in menu don't respect the environment value of .layoutDirection in SwiftUI
Problem Setting ".environment(.layoutDirection, .rightToLeft)" to a view programmatically won't make buttons in menu to show right to left. However, setting ".environment(.locale, .init(identifier: "he-IL"))" to a view programmatically makes buttons in menu to show Hebrew strings correctly. Development environment: Xcode 16.x, macOS 15.3.1 Target iOS: iOS 17 - iOS 18 The expected result is that the button in the menu should be displayed as an icon then a text from left to right. Code to demonstrate the problem: struct ContentView: View { var body: some View { VStack(alignment: .leading) { Text("Buttons in menu don't respect the environment value of .layoutDirection") .font(.subheadline) .padding(.bottom, 48) /// This button respects both "he-IL" of ".locale" and ".rightToLeft" of ".layoutDirection". Button { print("Button tapped") } label: { HStack { Text("Send") Image(systemName: "paperplane") } } Menu { /// This button respects "he-IL" of ".locale" but doesn't respect ".rightToLeft" of ".layoutDirection". Button { print("Button tapped") } label: { HStack { Text("Send") Image(systemName: "paperplane") } } } label: { Text("Menu") } } .padding() .environment(\.locale, .init(identifier: "he-IL")) .environment(\.layoutDirection, .rightToLeft) } }
4
0
76
Mar ’25
SwiftUI Button is not tappable if an Image is followed in a VStack
Here is the code: import SwiftUI struct ContentView: View { var body: some View { VStack(alignment: .leading, spacing: 8) { Button(action: { print("Tapped the button") }, label: { Text("Button") .foregroundStyle(Color.white) .padding() .background(Color.gray) }) .border(Color.green, width: 2) Image(systemName: "square.and.arrow.up") .resizable() .aspectRatio(contentMode: .fill) .foregroundStyle(Color.black) .frame(height: 240) .border(Color.blue, width: 2) .clipped() } .padding() .border(Color.black, width: 2) } } #Preview { ContentView() } The problem is that the button is not tappable. Test environment: macOS 15.3.2 (24D81), Xcode Version 16.2 (16C5032a), Simulator: iPhone 16 (18.3.1) However, if we change the code from ".aspectRatio(contentMode: .fill)" to ".aspectRatio(contentMode: .fit)" then the problem goes away. Any suggestions to fix the problem?
Topic: UI Frameworks SubTopic: SwiftUI
3
0
107
Apr ’25