zoukankan      html  css  js  c++  java
  • 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博客园看到本文,请点击查看原文.

  • 相关阅读:
    截取文件路径组成新文件路径
    jsoup选择器
    正则小示例
    一个没有经过优化的过滤指定目录下的指定扩展名文件的算法
    正则表达式生成问题
    链接中带换行的页面查找替换问题
    数组扩容测试
    LeetCode 3.无重复字符的最长子串
    LeetCode 200.岛屿数量
    LeetCode 560.和为K的子数组
  • 原文地址:https://www.cnblogs.com/wb145230/p/4573851.html
Copyright © 2011-2022 走看看