iOS将按钮添加到小部件扩展 [英] iOS Add Button to Widget Extension

查看:71
本文介绍了iOS将按钮添加到小部件扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的应用程序设计一个小部件,基本上,该小部件应包含三个按钮.到目前为止,我还没有找到任何有关如何向iOS Widget Extension添加按钮的教程.我看到例如Google Maps在其小部件中使用按钮.

I am currently designing a widget for my app and basically, the widget should consist of three buttons. So far, I didn't find any tutorial on how to add buttons to an iOS Widget Extension. I see that e.g. Google Maps uses buttons in their widgets.

这怎么办?

推荐答案

小部件 没有动画或自定义交互,但是我们可以深层链接从我们的 widget 进入我们的应用程序. systemSmall 小部件是一个较大的点击区域,而 systemMedium systemLarge 可以使用新的SwiftUI link API 创建可点击的小部件中的区域.

Widgets do not have animation or custom interactions, but we can deep link from our widget into our app. systemSmall widgets are one large tap area, while systemMedium and systemLarge can use the new SwiftUI link API to create tappable zones within the widget.

  1. 您的窗口小部件扩展名中.

    struct YourWidgetEntryView : View {
        var entry: Provider.Entry
        
        @Environment(\.widgetFamily) var family //<- here
        
        @ViewBuilder
        var body: some View {
            switch family {
            case .systemSmall:
                Text("Small") //.systemSmall widgets are one large tap area
            default:
                HStack{
                    Link(destination: URL(string: "game:///link1")!, label: {
                        Text("Link1")
                    })
                    Spacer()
                    Link(destination: URL(string: "game:///link2")!, label: {
                        Text("Link2")
                    })
                    Spacer()
                    Link(destination: URL(string: "game:///link3")!, label: {
                        Text("Link3")
                    })
                }
                .padding()
                
            }
        }
    }

  1. 在您的应用中

struct ContentView: View {
    
    @State var linkOne: Bool = false
    @State var linkTwo: Bool = false
    @State var linkThree: Bool = false
    
    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    destination: Text("Link1-View"), isActive: $linkOne) {
                    Text("Link1")
                }
                NavigationLink(
                    destination: Text("Link2-View"), isActive: $linkTwo) {
                    Text("Link2")
                }
                NavigationLink(
                    destination: Text("Link3-View"), isActive: $linkThree) {
                    Text("Link3")
                }
            }

            .navigationBarTitle("Links")
            .onOpenURL(perform: { (url) in
                self.linkOne = url == URL(string: "game:///link1")!
                self.linkTwo = url == URL(string: "game:///link2")!
                self.linkThree = url == URL(string: "game:///link3")!
            })
        }
    }
}

这篇关于iOS将按钮添加到小部件扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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