当前位置 博文首页 > xixi:创建一个 iOS 的 Widget (二)
—->>>>>创建一个 iOS 的 Widget (一)<<<<<—-
聊下 在项目里面 做展开和折叠
这个功能在iOS10 才出现的
并且样式 跟 iOS10 以下 是有点不一样的,
iOS9 的左边 是留了一定的间距,而iOS10 是在屏幕的两边留了大概 7 的宽度。
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:@"group.xixi"];
[def setObject:@"xixi" forKey:@"name"];
[def setObject:@18 forKey:@"age"];
[def setBool:NO forKey:@"isExpansion"]; //记录折叠 或者 展开
[def synchronize];
UILabel *leftLabel = [[UILabel alloc] init];
leftLabel.frame = CGRectMake(30, 50, 50, 50);
leftLabel.text = @"折叠";
leftLabel.textColor = [UIColor blackColor];
[self.view addSubview:leftLabel];
UISwitch *sh = [[UISwitch alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftLabel.frame), 50, 100, 40)];
[sh addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sh];
UILabel *rightLabel = [[UILabel alloc] init];
rightLabel.frame = CGRectMake(CGRectGetMaxX(sh.frame) + 10, 50, 50, 50);
rightLabel.text = @"展开";
rightLabel.textColor = [UIColor blackColor];
[self.view addSubview:rightLabel];
-(void)switchAction:(UISwitch *)switchArg
{
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:@"group.xixi"];
[def setBool:switchArg.on forKey:@"isExpansion"];
[def synchronize];
}
![图11](https://img-blog.csdn.net/20161112164224522) —
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:@"group.xixi"];
nameStr = [def objectForKey:@"name"];
age = [def objectForKey:@"age"];
isExpansion = [def boolForKey:@"isExpansion"];
_tableView = [[UITableView alloc] init];
_tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_tableView.backgroundColor = [UIColor greenColor];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[self setViewModel];
}
-(void) setViewModel
{
//这里你可以获取数据 然后获取数据之后 根据数据 来展现 你想要的Widget 模式
//get you data and reload tableview
//通过这个来展现数据
if (ios_version >= 10)
{
if (isExpansion)
{
//展开
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];
}
else
{
//折叠
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeCompact];
}
}
else
{
self.preferredContentSize = CGSizeMake(0, tableview_MaxHeight);
}
}
//ios10 下才有这个方法
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize
{
if (activeDisplayMode == NCWidgetDisplayModeCompact)
{
self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 105);
_tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, 105);
}
else
{
self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, tableview_MaxHeight);
_tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, tableview_MaxHeight);
}
}
补充下 : 怎样让iOS9以下的 Widget 的大小跟iOS10 一样,这样就不用做两套东西
//这个方法是让 左边的间距缩小
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
return UIEdgeInsetsZero;
}