iOS的Widget类似Android的Notification设置flags为Notification.FLAG_ONGOING_EVENT后

iOS Widget简单使用    iOS Widget简单使用

 

OK,大约知道是什么意思了,现在可以开始码了.

1.创建Widget

Xcode菜单 -> File -> New -> Target.. -> 选择Today Extension

iOS Widget简单使用  --> iOS Widget简单使用

 

2.在plist文件里设置纯代码的Widget

  1. 删掉NSExtensionMainStoryboard字段
  2. 添加NSExtensionPrincipalClass字段,设置value为TodayViewController,当然也可以设置其他的ViewController

iOS Widget简单使用

 

3.运行的时候选择Today

iOS Widget简单使用

 

4.添加一些控件看看效果

- (void)viewDidLoad {
    [super viewDidLoad];
    //  设置widget的高度
    self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 100);
    //  添加一个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];
}

 

效果如图

iOS Widget简单使用

 

看效果图你会发现,左边还空了一大块,原因是Widget默认会有一个inset,重写下面的方法就好了

-(UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
    return UIEdgeInsetsZero;
}

最终的效果如下:

iOS Widget简单使用

 

如果你不是在wb145230博客园看到本文,请点击查看原文.

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-08-13
  • 2022-02-28
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-10
  • 2021-06-12
  • 2021-04-25
  • 2021-12-16
  • 2022-12-23
相关资源
相似解决方案