Swiftui 中常见小问题合集

128 阅读1分钟

1. 在使用Button的时候,如何需要不可以点击的时候

  1. 自定义 ButtonStyle
  2. 设置默认的 ButtonStyle 样式为 .plain

2. 如何想使整个试图可以点击需要加一个样式

.contentShape(Rectangle())

3. TabView如何为空,会在 iOS 16 版本一下崩溃

if list.count > 0 {
    TabView{
        ForEach(list) { item in 
            Text(item)
        }
    }
} else {
    Color.red
}

4. LazyVStack 和 LazyVGrid 混用会有 bug.

5. 封装方法以使用最新的功能

extentsion View {
    @ViewBuilder
    func tintColor(color: Color) -> some View {
        if #available(iOS 16.0, *) {
            return tint(color)
        } else {
            return accentColor(color)
        }
    }
    @ViewBuilder
    func refreshableView(action: @Sendable @escaping () async -> Void) -> some View {
        if #available(iOS 15.0, *) {
            return refreshable(action: action)
        } else {
            return self
        }
    }
}


Xcode 报错问题

  1. 修改 build setting 里的 use script: False
  2. Enable Modules(C and Objective-C) : True
  3. pod 添加环境配置(build_settings)
post_install do |installer|
  installer.pods_project.targets.each do |target|
    
    # 添加以下配置来解决 libarclite 问题
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end

  # 解决模拟器上的 arm64 架构警告
  installer.pods_project.build_configurations.each do |config|
    config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
  end
end