如何创建在 iOS 10 的主屏幕上显示的小部件

2024-01-04

I need to create custom widgets on home screen in iPhone. I have attach the sample image. Please give me any idea which extension will be helpful in creating this.enter image description here


Swift3 和 Xcode8

对于上述内容,您需要为主应用程序创建一个扩展

  • 我需要创建哪个扩展才能实现上述目标?

    答:今天延长

  • 如何创建扩展?

    Xcode -> 文件 -> 新建 -> 目标 -> 今天扩展

创建扩展后,如果您想将数据从主应用程序传递到扩展程序,那么您需要了解应用程序组

AppGroups只不过是在主应用程序和扩展程序之间共享数据

如何实现?

就这么简单????

Go to Xcode -> Capabilities -> AppGroups Enable -> Click + -> Add a New Container with format group.*

例如:group.com.yourCompany.ProjectName

转到主应用程序

使用 Suite 初始化默认值

var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!

使用 Suite 初始化默认值并设置您需要传递给扩展的 ArrayData 或字符串数​​据后

appGroupDefaults.set(value: arrayDataToPasstoTodayExtension, forKey: "arrayDatatoDisplayInToday")

然后在扩展中检索数据后????

var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!
let dataArray = appGroupDefaults.value(forKey: "arrayDatatoDisplayInToday")! as! NSMUtableArray
Print("Hurray i got the data from Main App to Extension")

扩展中

For ShowLess and ShowMoreWidget 中的更多选项[今天]

override func viewDidLoad() {
    self.preferredContentSize = CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 )

    if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    } else {
        // Fallback on earlier versions
    }
} 
// For iOS 10
@available(iOS 10.0, *)
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
    self.preferredContentSize = (activeDisplayMode == .expanded) ? CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 ) : CGSize(width: maxSize.width, height: 90)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何创建在 iOS 10 的主屏幕上显示的小部件 的相关文章

随机推荐