iOS 添加按钮到 WidgetKit 扩展

标签 ios button widget widgetkit

我目前正在为我的应用程序设计一个 WidgetKit ,基本上, WidgetKit 应该由三个按钮组成。到目前为止,我没有找到任何关于如何向 iOS Widget Extension 添加按钮的教程。我看到例如Google map 在其 WidgetKit 中使用按钮。
如何才能做到这一点?

最佳答案

Widgets 没有动画或自定义交互 ,但我们可以从我们的 widget 深层链接进入我们的应用程序。 systemSmall WidgetKit 是一个大的点击区域,而 systemMediumsystemLarge可以使用新的 SwiftUI link API在 WidgetKit 内创建可点击区域。
enter image description here

演示 ;使用 Link

  • 在您的 WidgetKit 扩展中。

  •     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()
                    
                }
            }
        }
    
    
  • 在您的应用程序中

  • 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 添加按钮到 WidgetKit 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65556274/

    相关文章:

    ios - 使 UIBezierPath 可选择并更改其颜色

    iphone - iOS Enterprise 分发警告消息

    jquery - 如何在 jQuery 中单击按钮时触发 Accordion?

    javascript - 如何向页面添加新的 Dijit 小部件?

    flutter - Flutter 中具有最小高度的扩展小部件

    android - 谷歌 OAuth2 : perform token refreshing server-side while refresh token or authorization code acquired via mobile app

    iphone - 在 Xcode 中组织不同版本的应用程序

    forms - 带有两个提交按钮的 Symfony 2 表单

    c++ - 为什么我的窗口中的按钮在 C++ 中看起来很旧?

    widget - Google Blogger/Blogspot 和创建自定义容器以将小工具小部件添加到布局页面