【iOS小组件】iOS17与低版本兼容适配

背景

当使用 XCode15.3 创建小组件时默认使用的都是最新语法和特性的示例模版,预览与运行一切正常。

当我们切换到低版本(如iOS14)后灾难出现了,代码一片红😢,即无法预览也无法运行😰。

image.png

image.png

前面【iOS小组件】第一个小组件 已经解决了,为了避免踩坑,这里描述一下遇到的错误与对应的兼容处理方案。

第一步:处理预览报错

预览示例中使用的 APIiOS17 开始支持的,当我们切换到低版本肯定会报错

image.png

🧰 处理方案就是改为低版本 API 写法。

定义一个 struct 继承自 PreviewProvider 并提供必要的 previewContext

//#Preview(as: .systemSmall) {
//    MyWidget()
//} timeline: {
//    SimpleEntry(date: .now, emoji: "😀")
//    SimpleEntry(date: .now, emoji: "🤩")
//}

struct MyWidgetEntryView_Previews: PreviewProvider {
    static var previews: some View {
        MyWidgetEntryView(entry: SimpleEntry(date: Date(), emoji: "😁"))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    }
}

重新预览,此时预览已正常,接下来处理 background 的兼容问题

image.png

第二步:处理背景兼容

从报错信息上可以看到,报错原因是因为没有使用 containerBackground

image.png

containerBackgroundiOS 17 中新增的接口,用于统一设置View背景颜色。如果你的小组件使用了 background 设置背景颜色,可能需要适配新的 containerBackground API

适配方法就是将 background 替换为containerBackground

兼容处理可以通过给 View 添加扩展方法解决:

extension View {
    
    @ViewBuilder
    func widgetBackground(_ backgroundView: some View) -> some View {
        
        if #available(iOS 17.0, *) {
            containerBackground(for: .widget) {
                backgroundView
            }
        } else {
            background(backgroundView)
        }
    }
    
    func widgetBackground2(_ backgroundView: some View) -> some View {
        
        if #available(iOS 17.0, *) {
            return containerBackground(for: .widget) {
                backgroundView
            }
        } else {
            return background(backgroundView)
        }
    }
}

上面提供了两种处理方式,第一种是通过 视图构造器,第二种是通过 扩展方法,使用哪种都行。

在设置背景颜色的时候就可以直接使用这个方法就行:

struct MyWidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        VStack {
            Text("Time:")
            Text(entry.date, style: .time)

            Text("Emoji:")
            Text(entry.emoji)
        }
        
        .widgetBackground(Color.white)
    }
}

struct MyWidget: Widget {
    let kind: String = "MyWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in







            
            
            MyWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

完整代码

extension View {
    
    @ViewBuilder
    func widgetBackground(_ backgroundView: some View) -> some View {
        
        if #available(iOS 17.0, *) {
            containerBackground(for: .widget) {
                backgroundView
            }
        } else {
            background(backgroundView)
        }
    }
    
    func widgetBackground2(_ backgroundView: some View) -> some View {
        
        if #available(iOS 17.0, *) {
            return containerBackground(for: .widget) {
                backgroundView
            }
        } else {
            return background(backgroundView)
        }
    }
}

struct MyWidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        VStack {
            Text("Time:")
            Text(entry.date, style: .time)

            Text("Emoji:")
            Text(entry.emoji)
        }
        .widgetBackground(Color.white)
    }
}

struct MyWidget: Widget {
    let kind: String = "MyWidget"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            
            MyWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
    }
}

struct MyWidgetEntryView_Previews: PreviewProvider {
    static var previews: some View {
        MyWidgetEntryView(entry: SimpleEntry(date: Date(), emoji: "😁"))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
        MyWidgetEntryView(entry: SimpleEntry(date: Date(), emoji: "😁"))
            .previewContext(WidgetPreviewContext(family: .systemMedium))
    }
}

友情链接

Swift 官方教程:

developer.apple.com/swift

SwiftUI 官方教程:

developer.apple.com/tutorials/s…

本文同步自微信公众号 “程序员小溪” ,这里只是同步,想看及时消息请移步我的公众号,不定时更新我的学习经验。

阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.shuli.cc/?p=20232,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?