圖片1(創(chuàng)建今日擴(kuò)展)

圖片2

圖片3(設(shè)置大小)

圖片4(繪畫(huà)控件)

圖片5(設(shè)置共享文件)

圖片6(設(shè)置群組ID)

圖片7(設(shè)置URL Schemes)

擴(kuò)展中的主要邏輯代碼
import UIKit
class TodayViewController: UIViewController, NCWidgetProviding,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
//var newsList = [NewsModel]()
lazy var moreBtn:UIButton = {
let btn = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
btn.setTitle("查看更多", for:.normal)
btn.addTarget(self, action: #selector(gotoMainApp), for: .touchUpInside)
btn.backgroundColor = UIColor(red:245/255.0, green:74/255.0, blue:48/255.0, alpha: 1)
return btn
}()
func gotoMainApp(){
//跳轉(zhuǎn)到主程序的代碼(見(jiàn)圖7)
//self.extensionContext?.open(URL(string:"WidgetApp://action=GotoNewsListPage")!, completionHandler: { (suc:Bool) in
})
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.tableView.delegate = self
self.tableView.dataSource = self
//注冊(cè)自定義cell
let nib = UINib(nibName: "NewsInfoCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "NewsInfoCell")
updateView()
}
//刷新界面
func updateView(){
if newsList.count != 0{
newsList.removeAll()
}
//通過(guò)UserDefaults從組里面獲取共享數(shù)據(jù)(見(jiàn)圖6)
let ud = UserDefaults(suiteName: "group.xxx");
//if let nickName = ud?.array(forKey: "group.xxx.XXX"){
// for (_,element) in nickName.enumerated() {
// if let edic = element as? [String : Any]{
// let nm = NewsModel()
// nm.setValuesForKeys(edic)
// newsList.append(nm)
// }
// }
self.tableView.reloadData()
if newsList.count != 0 {
self.tableView.tableFooterView = self.moreBtn
}
//判斷在不同數(shù)據(jù)下展示界面的視圖大小(不處理的話(huà),展開(kāi)和折疊會(huì)出問(wèn)題)
if #available(iOSApplicationExtension 10.0, *) {
if extensionContext?.widgetActiveDisplayMode == .compact{ //壓縮狀態(tài)
//
}else{//展開(kāi)狀態(tài)
if newsList.count != 0 {
UIView.animate(withDuration: 0.1, animations: {
// self.preferredContentSize = CGSize(width: self.view.bounds.width, height: CGFloat(self.newsList.count*100 + 44))
})
}
}
} else {
// Fallback on earlier versions
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//設(shè)置今日擴(kuò)展模式為可張開(kāi)收縮模式(IOS10以后支持,進(jìn)入擴(kuò)展右上角顯示"展開(kāi)"和"折疊")
if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
} else {
// Fallback on earlier versions
};
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return newsList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//新聞列表
//let cell = tableView.dequeueReusableCell(withIdentifier: "NewsInfoCell") as! NewsInfoCell
//cell.news = newsList[indexPath.row]
//return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//let url = newsList[indexPath.row].ShowNewsUrl
// extensionContext?.open(URL(string:"WidgetApp://action=Goto-\(url)")!, completionHandler: { (suc:Bool) in
// })
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//return 95;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//監(jiān)聽(tīng)擴(kuò)展的展開(kāi)和收縮狀態(tài)處理視圖的大小
/**
* @maxSize 界面能夠顯示的最小和最大尺寸(最小狀態(tài)下是110)
* @activeDisplayMode: 張開(kāi)和收縮狀態(tài)
**/
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if(activeDisplayMode == .compact){
//self.preferredContentSize = CGSize(width: maxSize.width, height: maxSize.height)
}else{
//self.preferredContentSize = CGSize(width: maxSize.width, height: CGFloat(newsList.count*100 + 44))
}
}
//自行看官方注釋
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
if #available(iOSApplicationExtension 10.0, *) {
if extensionContext?.widgetActiveDisplayMode == .compact{
completionHandler(NCUpdateResult.newData)
}else{
completionHandler(NCUpdateResult.noData)
}
} else {
// Fallback on earlier versions
completionHandler(NCUpdateResult.newData)
}
}
}
收縮狀態(tài)
展開(kāi)狀態(tài)

