iOS的Widget类似Android的Notification设置flags为Notification.FLAG_ONGOING_EVENT后
images/loading.gif' data-original="http://images0.cnblogs.com/blog2015/423172/201506/131723432076654.png" width="341" height="619" />
OK,大约知道是什么意思了,现在可以开始码了.
1.创建Widget
Xcode菜单 -> File -> New -> Target.. -> 选择Today Extension
-->
2.在plist文件里设置纯代码的Widget
- 删掉NSExtensionMainStoryboard字段
- 添加NSExtensionPrincipalClass字段,设置value为TodayViewController,当然也可以设置其他的ViewController
3.运行的时候选择Today
4.添加一些控件看看效果
- (void)viewDidLoad { [super viewDidLoad]; //添加一个button,点击button后改变背景的颜色 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"点击一下" forState:UIControlStateNormal]; button.backgroundColor = [UIColor whiteColor]; [button addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(50, 10, 100, 30); [self.view addSubview:button]; }- (void)clickAction{ self.view.backgroundColor = [UIColor redColor];}
效果如图
看效果图你会发现,左边还空了一大块,原因是Widget默认会有一个inset,重写下面的方法就好了
-(UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets { return UIEdgeInsetsZero;}
最终的效果如下:
如果你不是在wb145230博客园看到本文,请点击查看原文.
原标题:iOS Widget简单使用
关键词:IOS