iOS的Widget类似Android的Notification设置flags为Notification.FLAG_ONGOING_EVENT后
OK,大约知道是什么意思了,现在可以开始码了.
1.创建Widget
Xcode菜单 -> File -> New -> Target.. -> 选择Today Extension
-->
2.在plist文件里设置纯代码的Widget
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; }
最终的效果如下: