简体   繁体   English

列表上下文中 SwiftUI 按钮的样式差异:iOS 15 与更早版本

[英]Styling differences in SwiftUI Button in list context: iOS 15 vs. earlier

With this example:通过这个例子:

struct ContentView: View {
    let items = ["Hello, world!"]
    var body: some View {
        List {
            Button(action: {
            }, label: {
                Text("Hello, world!")
            })
            .listRowBackground(Color.gray)
        }
    }
}

on iOS 14, this looks like:在 iOS 14 上,这看起来像:

在此处输入图片说明

on iOS 15, it now looks like:在 iOS 15 上,它现在看起来像:

在此处输入图片说明

It is rather surprising the number of styling differences:样式差异的数量相当令人惊讶:

  1. The corner rounding: In iOS 15, but not earlier圆角:在 iOS 15 中,但不是更早
  2. The text color: In iOS 15, blue and white earlier.文字颜色:在 iOS 15 中,更早的时候是蓝色和白色。
  3. The width of the view-- considerably narrower in iOS 15视图的宽度——在 iOS 15 中相当窄

Is this just an expected change with iOS 15?这只是 iOS 15 的预期变化吗?

Is there a standard way to not have these changes occur?有没有标准的方法来避免这些变化发生? Ie, to retain the earlier styling?即,保留较早的样式?

Yes, Apple has changed its default UI elements states.是的,Apple 已更改其默认 UI 元素状态。 This can also be seen in Safari where webpages have no CSS for forms.这也可以在 Safari 中看到,其中网页没有用于表单的 CSS。

The specific answer that helped me was the use of .listStyle .帮助我的具体答案是使用.listStyle In my original code, I didn't use .listStyle , and apparently the default has changed.在我的原始代码中,我没有使用.listStyle ,显然默认值已更改。 So, if I do:所以,如果我这样做:

struct ContentView: View {
    let items = ["Hello, world!"]
    var body: some View {
        List {
            Button(action: {
            }, label: {
                Text("Hello, world!")
            })
            .listRowBackground(Color.gray)
        }
        .listStyle(PlainListStyle())
    }
}

I get a result closer to what I want.我得到了更接近我想要的结果。

Apparently the .listStyle default is now insetGrouped , which is not what I wanted.显然.listStyle默认值现在是insetGrouped ,这不是我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 15 中的 SwiftUI 垂直列表部分标题填充 - SwiftUI Vertical List Section header padding in iOS 15 iOS 15:在 SwiftUI 中使用 UISheetPresentationController - iOS 15: using UISheetPresentationController in SwiftUI SwiftUI - 无法在 iOS 15 中设置列​​表的背景颜色(适用于 iOS 14) - SwiftUI - Unable to set background color of List in iOS 15 (works in iOS 14) 如何使用 header 获得 SwiftUI 列表部分的正确布局,页脚在 iOS 15 上运行,但使用 iOS 14 SDK 构建? - How to get correct layout of SwiftUI List Section with header and footer run on iOS 15, but built with iOS 14 SDK? 用于 HTML 输入表单的 iOS 键盘中的 Go 与返回按钮 - Go vs. return button in iOS keyboard for HTML input forms 如何在 iOS 15 及更早版本上支持程序化视图关闭 - How to support programmatic view dismiss on iOS 15 and earlier versions 小部件未显示在 iOS 15 或更早版本的 Xcode 14 中 - Widgets not showing in iOS 15 or earlier from Xcode 14 在iOS中的持久性:属性列表序列化与NSUserDefaults与核心数据 - Persistence in iOS: Property List Serialization vs. NSUserDefaults vs. Core Data SwiftUI 导航立即驳回意见? iOS 15 - SwiftUI Navigation immediately dismisses views? iOS 15 SwiftUI 中的预填充文本字段在 iOS 15 中被破坏 - Prefill textfields in SwiftUI is broken in iOS 15
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM