iOS 14小部件:如何为每个小部件尺寸系列创建不同的布局? [英] iOS 14 Widgets: How to create different layouts for every widget size family?

查看:147
本文介绍了iOS 14小部件:如何为每个小部件尺寸系列创建不同的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每种小部件尺寸(即小,中,大)创建不同的布局.如何根据窗口小部件的大小来分支代码?

I want to create different layouts for each widget size (i.e. small, medium, large). How can I branch my code according to widget's size?

推荐答案

WidgetFamily( Apple作为WidgetKit一部分的Documentation )枚举,您可以在视图中切换各种尺寸并进行相应调整.将其设置为@Environment变量并打开可用的情况:

The WidgetFamily (Apple Documentation) enum as part of WidgetKit will allow you to switch upon the various sizes within your view and adjust accordingly. Set this as an @Environment variable and switch on the avaliable cases:

  • .systemSmall
  • .systemMedium
  • .systemLarge
  • .systemSmall
  • .systemMedium
  • .systemLarge
struct WidgetView : View {
   @Environment(\.widgetFamily) var family

    @ViewBuilder
    var body: some View {
        
        switch family {
        case .systemSmall:
            Text("Small")
        case .systemMedium:
            Text("Medium")
        case .systemLarge:
            Text("Large")
        default:
            Text("Some other WidgetFamily in the future.")
        }

    }
}

这篇关于iOS 14小部件:如何为每个小部件尺寸系列创建不同的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆