环境:iOS 10.3  ,xcode8.3.2

创建widget:add target 


ios widget 与 3D touch


ios widget 与 3D touch


创建后 拖控件到storyboard 上 ios10 可设置展开折叠模式

ios widget 与 3D touch

ios widget 与 3D touch


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {

        self.extensionContext.widgetLargestAvailableDisplayMode = NCWidgetDisplayModeExpanded;

        //        NCWidgetDisplayModeCompact

    }


    [self.extensionContext setValue:@"1" forKey:@"widgetLargestAvailableDisplayMode"];

}


-(void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize{

    if (activeDisplayMode == NCWidgetDisplayModeCompact) {

        

        NSLog(@"maxSize-%@",NSStringFromCGSize(maxSize));// maxSize-{359, 110}

        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);

    }else{

        NSLog(@"maxSize-%@",NSStringFromCGSize(maxSize));// maxSize-{359, 616}

        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 258);

    }

}


收起时110高度,展开时可以自定义 这里是258







ios widget 与 3D touch

ios widget 与 3D touch



添加3D touch

在主project 种选择.plist 文件添加节点

<key>UIApplicationShortcutItems</key>

<array>

<dict>

<key>UIApplicationShortcutItemTitle</key>

<string>我的3d</string>

<key>UIApplicationShortcutItemIconType</key>

<string></string>

<key>UIApplicationShortcutItemType</key>

<string></string>

<key>UIApplicationShortcutItemUserInfo</key>

<dict/>

</dict>

</array>


或者

ios widget 与 3D touch


运行效果

ios widget 与 3D touch




具体数据交互要设置 app group 等等,这里不再赘述,此篇博客作为个人学习记录




相关文章: