擴(kuò)展(Extension)是iOS 8中引入的一個(gè)非常重要的新特性。擴(kuò)展讓app之間的數(shù)據(jù)交互成為可能。用戶可以在app中使用其他應(yīng)用提供的功能,而無需離開當(dāng)前的應(yīng)用。iOS 8系統(tǒng)有6個(gè)支持?jǐn)U展的系統(tǒng)區(qū)域,分別是Today、Share、Action、Photo Editing、Storage Provider、Custom keyboard。支持?jǐn)U展的系統(tǒng)區(qū)域也被稱為擴(kuò)展點(diǎn)。對于賽事比分,股票、天氣、快遞這類需要實(shí)時(shí)獲取的信息,可以在通知中心的Today視圖中創(chuàng)建一個(gè)Today擴(kuò)展實(shí)現(xiàn)。Today擴(kuò)展又稱為Widget。本文主要是介紹Today Extension的用法。
創(chuàng)建步驟1、選擇File–>New–>Target創(chuàng)建一個(gè)新的Target,如下圖所示:iOS8_today_extension2、選擇Application Extension–>Today Extension(下圖中最后一個(gè)),指定需要?jiǎng)?chuàng)建的Extension的類型,這里選擇Today Extension,如下圖所示:ios8_today_extension3、按照要求填寫Product Name、Organization Name和Language,如下圖所示:ios8_today_extension4、點(diǎn)擊Finish后Xcode會(huì)提示是否需要立即激活當(dāng)前創(chuàng)建的Scheme,默認(rèn)選擇cancel就行了,Xcode會(huì)自動(dòng)創(chuàng)建一個(gè)Today Extension的Target,默認(rèn)會(huì)創(chuàng)建如下幾個(gè)文件:TodayViewController.h/TodayViewController.mMainInterface.storyboardSupporting Files/Info.plist5、啟動(dòng)應(yīng)用后在通知中心添加就能看到運(yùn)行效果,如下圖所示:ios8_today_extension_004.png開發(fā)實(shí)例1、配置Info.plist文件,使用代碼布局Xcode6中新建的Today Extension默認(rèn)是使用StoryBoard布局的,如果需要使用代碼布局就需要配置Info.plist文件,步驟如下:(1)刪除NSExtensionMainStoryboard的配置,如下所示:12NSExtensionMainStoryboardMainInterface(2)添加NSExtensionPrincipalClass的配置,如下所示:12NSExtensionPrincipalClassTodayViewController(3)修改CFBundleDisplayName,調(diào)整Widget顯示的名稱12CFBundleDisplayName顯示的名稱2、調(diào)整Widget的高度self.preferredContentSize = CGSizeMake(0, 200);取消widget默認(rèn)的inset,讓應(yīng)用靠左1234- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets{? ? return UIEdgeInsetsZero;}3、示例運(yùn)行效果如下:ios8_today_extension_005.png布局代碼如下:【以下代碼使用了Masonry來進(jìn)行AutoLayout處理】
?(void)makeView{? ? __weak __typeof(&*self)ws = self;? ? //內(nèi)容視圖? ? self.contentView = [UIView new];? ? self.contentView.backgroundColor = [UIColor clearColor];? ? [self.view addSubview:self.contentView];? ? self.contentView.userInteractionEnabled = YES;? ? [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {? ? ? ? make.edges.equalTo(ws.view);? ? ? ? make.height.mas_equalTo(@44).priorityHigh();? ? }];? ? //Label1? ? self.priceLabel = [UILabel new];? ? self.priceLabel.textColor = [UIColor colorWithRed:66.0/255 green:145.0/255 blue:211.0/255 alpha:1];? ? self.priceLabel.text = @"$592.12";? ? [self.contentView addSubview:self.priceLabel];? ? [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {? ? ? ? make.left.equalTo(ws.contentView.mas_left).with.offset(20);? ? ? ? make.top.equalTo(ws.contentView.mas_top).with.offset(10);? ? ? ? make.size.mas_equalTo(CGSizeMake(62, 21));? ? }];? ? //切換按鈕? ? self.toggleLineChartButton = [UIButton buttonWithType:UIButtonTypeCustom];? ? [self.contentView addSubview:self.toggleLineChartButton];? ? [self.toggleLineChartButton addTarget:self action:@selector(toggleLineChartViewVisible:) forControlEvents:UIControlEventTouchUpInside];? ? [self.toggleLineChartButton setImage:[UIImage imageNamed:@"caret-notification-center"] forState:UIControlStateNormal];? ? [self.toggleLineChartButton mas_makeConstraints:^(MASConstraintMaker *make) {? ? ? ? make.right.equalTo(ws.contentView.mas_right).with.offset(0);? ? ? ? make.top.equalTo(ws.contentView.mas_top).with.offset(0);? ? ? ? make.size.mas_equalTo(CGSizeMake(44, 44));? ? }];? ? //Label2? ? self.priceChangeLabel = [UILabel new];? ? self.priceChangeLabel.textColor = [UIColor colorWithRed:133.0/255 green:191.0/255 blue:37.0/255 alpha:1];? ? self.priceChangeLabel.text = @"+1.23";? ? [self.contentView addSubview:self.priceChangeLabel];? ? [self.priceChangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {? ? ? ? make.right.equalTo(ws.toggleLineChartButton.mas_left).with.offset(-20);? ? ? ? make.top.equalTo(ws.contentView).with.offset(10);? ? ? ? make.size.mas_equalTo(CGSizeMake(44, 21));? ? }];? ? //Label3? ? self.detailLabel = [[UILabel alloc] init];? ? self.detailLabel.backgroundColor = [UIColor clearColor];? ? self.detailLabel.numberOfLines = 0;? ? [self.contentView addSubview:self.detailLabel];? ? self.detailLabel.text = @"\n\n詳細(xì)信息";? ? self.detailLabel.textAlignment = NSTextAlignmentCenter;? ? [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {? ? ? ? make.top.equalTo(ws.priceLabel.mas_bottom).with.offset(0);? ? ? ? make.left.equalTo(ws.contentView);? ? ? ? make.width.equalTo(ws.contentView);? ? ? ? if(self.lineChartIsVisible)? ? ? ? {? ? ? ? ? ? self.detailLabel.textColor = [UIColor whiteColor];? ? ? ? ? ? make.height.mas_equalTo(@98);? ? ? ? }? ? ? ? else? ? ? ? {? ? ? ? ? ? self.detailLabel.textColor = [UIColor clearColor];? ? ? ? ? ? make.height.mas_equalTo(@0);? ? ? ? }? ? }];}
按鈕點(diǎn)擊處理:- (void)toggleLineChartViewVisible:(id)sender{? ? __weak __typeof(&*self)ws = self;? ? //重新布局? ? if(self.lineChartIsVisible)? ? {? ? ? ? self.preferredContentSize = CGSizeMake(0, 44);? ? ? ? [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {? ? ? ? ? ? make.height.mas_equalTo(@0);? ? ? ? ? ? ws.detailLabel.textColor = [UIColor clearColor];? ? ? ? ? ? ws.detailLabel.textAlignment = NSTextAlignmentCenter;? ? ? ? }];? ? ? ? //改變按鈕的方向? ? ? ? self.toggleLineChartButton.transform = CGAffineTransformMakeRotation(0);? ? ? ? self.lineChartIsVisible = NO;? ? ? ? [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {? ? ? ? ? ? make.height.mas_equalTo(@44).priorityHigh();? ? ? ? }];? ? }? ? else? ? {? ? ? ? self.preferredContentSize = CGSizeMake(0, 142);? ? ? ? [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {? ? ? ? ? ? make.height.mas_equalTo(@98);? ? ? ? ? ? ws.detailLabel.textColor = [UIColor whiteColor];? ? ? ? ? ? ws.detailLabel.textAlignment = NSTextAlignmentCenter;? ? ? ? }];? ? ? ? //改變按鈕的方向? ? ? ? self.toggleLineChartButton.transform = CGAffineTransformMakeRotation(180.0 * M_PI/180.0);? ? ? ? self.lineChartIsVisible = YES;? ? ? ? [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {? ? ? ? ? ? make.height.mas_equalTo(@142).priorityHigh();? ? ? ? }];? ? }}
代碼:https://github.com/pony2323/TodayExtention