iOS Widget简单使用

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

    

 

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

1.创建Widget

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

  --> 

 

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

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

 

3.运行的时候选择Today

 

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];
}

 

效果如图

 

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

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

最终的效果如下:

 

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

 

转载于:https://www.cnblogs.com/wb145230/p/4573851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值