• SettingsView/设置页 的实现


    1. 创建设置视图 SettingsView.swift

    1. import SwiftUI
    2. /// 设置页面
    3. struct SettingsView: View {
    4. /// 环境变量,呈现方式:显示或者关闭
    5. @Environment(\.presentationMode) var presentationMode
    6. /// 默认网址
    7. let defaultURL = URL(string: "https://www.google.com.hk")!
    8. // https://www.youtube.com/c/swiftfulthinking
    9. let youtubeURL = URL(string: "https://www.youtube.com")!
    10. // https://www.buymeacoffee.com/nicksarno
    11. let coffeeURL = URL(string: "https://www.buymeacoffee.com")!
    12. /// 交易货币网址
    13. let coingeckoURL = URL(string: "https://www.coingecko.com")!
    14. /// 个人网站 https://www.nicksarno.com
    15. let personalURL = URL(string: "https://blog.csdn.net/u011193452")!
    16. var body: some View {
    17. NavigationView {
    18. ZStack {
    19. // 背景
    20. Color.theme.background
    21. .ignoresSafeArea()
    22. // 内容
    23. List {
    24. // 应用介绍及网址 部分
    25. swiftfulThinkingSection
    26. .listRowBackground(Color.gray.opacity(0.2))
    27. //.listRowBackground(Color.theme.background.opacity(0.5))
    28. // 交易货币介绍及网址 部分
    29. coinGeckoSection
    30. .listRowBackground(Color.gray.opacity(0.2))
    31. // 开发者及网址 部分
    32. developerSection
    33. .listRowBackground(Color.gray.opacity(0.2))
    34. // 应用网址 部分
    35. applicationSection
    36. .listRowBackground(Color.gray.opacity(0.2))
    37. }
    38. .modifier(ListBackgroundModifier())
    39. }
    40. .font(.headline)
    41. .accentColor(.blue)
    42. .listStyle(.grouped)
    43. .navigationTitle("Settings")
    44. .toolbar {
    45. ToolbarItem(placement: .navigationBarLeading) {
    46. XMarkButton(presentationMode: presentationMode)
    47. }
    48. }
    49. }
    50. }
    51. }
    52. extension SettingsView{
    53. /// 应用介绍及网址 部分
    54. private var swiftfulThinkingSection: some View{
    55. Section {
    56. VStack(alignment: .leading) {
    57. Image("logo")
    58. .resizable()
    59. .frame(width: 100, height: 100)
    60. .clipShape(RoundedRectangle(cornerRadius: 20))
    61. Text("This app was made by following a @SwiftfulThinking course on YouTube. It uses MVVM Architecture, Combine, and CoreData!")
    62. .font(.callout)
    63. .fontWeight(.medium)
    64. .foregroundColor(Color.theme.accent)
    65. }
    66. .padding(.vertical)
    67. // 跳转网址
    68. Link("Subscribe on YouTube 🎉", destination: youtubeURL)
    69. Link("Support his coffee addiction ☕️", destination: coffeeURL)
    70. } header: {
    71. Text("Swiftful Thinking")
    72. }
    73. }
    74. /// 交易货币介绍及网址 部分
    75. private var coinGeckoSection: some View{
    76. Section {
    77. VStack(alignment: .leading) {
    78. Image("coingecko")
    79. .resizable()
    80. .scaledToFit()
    81. .frame(height: 100)
    82. .clipShape(RoundedRectangle(cornerRadius: 20))
    83. Text("The cryptocurrency data that is used in this app comes from a free API from CoinGecko! Prices may be slightly delayed.")
    84. .font(.callout)
    85. .fontWeight(.medium)
    86. .foregroundColor(Color.theme.accent)
    87. }
    88. .padding(.vertical)
    89. // 跳转网址
    90. Link("Visit CoinGecko 🦎", destination: coingeckoURL)
    91. } header: {
    92. Text("CoinGecko")
    93. }
    94. }
    95. /// 开发者及网址 部分
    96. private var developerSection: some View{
    97. Section {
    98. VStack(alignment: .leading) {
    99. Image("logo-transparent")
    100. .resizable()
    101. .frame(width: 100, height: 100)
    102. .clipShape(RoundedRectangle(cornerRadius: 20))
    103. Text("This app was developed by Nick. It uses SwiftUI and is written 100% in Swift. The project benefits from multi-threading, publishers/subscribers, and data persistance.")
    104. .font(.callout)
    105. .fontWeight(.medium)
    106. .foregroundColor(Color.theme.accent)
    107. }
    108. .padding(.vertical)
    109. // 跳转网址
    110. Link("Visit Website 🤙", destination: personalURL)
    111. } header: {
    112. Text("Developer")
    113. }
    114. }
    115. /// 应用网址 部分
    116. private var applicationSection: some View{
    117. Section {
    118. // 跳转网址
    119. Link("Terms of Service", destination: defaultURL)
    120. Link("Privacy Policy", destination: defaultURL)
    121. Link("Company Website", destination: defaultURL)
    122. Link("Learn More", destination: defaultURL)
    123. } header: {
    124. Text("Application")
    125. }
    126. }
    127. }
    128. /// 适配 iOS 16 ListView 背景修改问题
    129. struct ListBackgroundModifier: ViewModifier {
    130. @ViewBuilder
    131. func body(content: Content) -> some View {
    132. if #available(iOS 16.0, *) {
    133. content.scrollContentBackground(.hidden)
    134. } else {
    135. content
    136. }
    137. }
    138. }
    139. struct SettingsView_Previews: PreviewProvider {
    140. static var previews: some View {
    141. SettingsView()
    142. }
    143. }

    2. 效果图:

  • 相关阅读:
    计算机毕业设计Django毕业设计论文源代码酒店住房管理系统
    著者 许少辉 著. 乡村振兴战略下传统村落文化旅游设计 [专著] / 许少辉著中文文献总库 - 北京交通大学馆藏辉少许
    Apache开启SSL(https)访问网站配置
    Linux中DNS的正向和反向解析
    封装element的dialog组件
    数千年中东文明史的五个阶段
    08-黑马点评项目发布笔记和查看笔记功能的实现
    聊聊Vuex原理
    异构体之间通信
    测试人生 | 97年双非学历的小哥哥,2线城市涨薪100%,我酸了......
  • 原文地址:https://blog.csdn.net/u011193452/article/details/133764732