ios SwiftUI按钮禁用样式在某些情况下无效

yptwkmov  于 2023-01-03  发布在  iOS
关注(0)|答案(2)|浏览(114)

我在一个自定义视图中使用了以下按钮,并在多个位置重复使用

Button(action: { selectedDate = date }) {
                    VStack {
                        Text(day.shortName)
                            .font(.caption2)
                            .foregroundColor(isSelectedDate ? .white : .primary)
                            .frame(maxWidth: .infinity)
                        
                        Spacer().frame(height: 7)
                        
                        Text("\(date.dayOfMonth)")
                            .bold()
                            .foregroundColor(isSelectedDate ? .white : .primary)
                            .frame(maxWidth: .infinity)
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color.purple.brightness(isSelectedDate ? 0 : 0.6))
                    .clipped()
                }.disabled(isInPast)

过去的日期应该被禁用,而我确认它们实际上如预期的那样被禁用;然而,禁用的样式在多个屏幕中看起来不同,尽管它使用的是完全相同的视图。
什么原因会导致禁用状态在某些屏幕中没有相应的样式?
在两个屏幕截图中,日期25-29均被禁用

在这两种用法中,我只是将视图添加到VStack

var body: some View {
    VStack {
        WeekView(selectedDate: $booking.selectedDate).padding()

var body: some View {
    VStack(spacing: 0) {
        WeekView(selectedDate: $selectedDate)
            .padding(.horizontal)
            .padding(.bottom)
8yparm6h

8yparm6h1#

不要依赖于primary颜色。使用自定义颜色和isInPast来制作不透明度。

ukdjmx9f

ukdjmx9f2#

我发现,其中一个屏幕中的父对象具有.buttonStyle(.plain),这使得禁用的样式可以按预期工作,所以我只是将其添加到组件本身,以确保禁用的样式始终处于适当位置

相关问题