2017-01-26 131 views
3

這樣的描述說我正在一個iOS應用程序,並希望爲它創建一個Today Extension Widget。我的問題是我想要將高度改爲200.在研究我找到的唯一解決方案之後,我使用preferredContentSize atribute,但這不適用於我。Swift Today Extension preferredContentSize not resizing

我想添加一個tableView到它,我想它應該完全顯示。 我添加了viewDidLoad方法,以便您可以看到表的創建和添加位置。

override func viewDidLoad() { 
    super.viewDidLoad() 

    let myDefaults = UserDefaults(suiteName: "group.com.iOSApp")! 
    let eventData = myDefaults.object(forKey: "events") 

    if eventData != nil { 
     shownEvents = NSKeyedUnarchiver.unarchiveObject(with: eventData as! Data) as! [Event] 
    } 

    eventTable = UITableView() 
    eventTable.register(TodayViewCell.self, forCellReuseIdentifier: "cell") 
    eventTable.separatorColor = UIColor.primary() 
    view.addSubview(eventTable) 
    eventTable.translatesAutoresizingMaskIntoConstraints = false 

    var tempX = NSLayoutConstraint(item: eventTable, attribute: .leading, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0) 
    var tempY = NSLayoutConstraint(item: eventTable, attribute: .top, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .top, multiplier: 1, constant: 0) 
    NSLayoutConstraint.activate([tempX, tempY]) 

    tempX = NSLayoutConstraint(item: eventTable, attribute: .trailing, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0) 
    tempY = NSLayoutConstraint(item: eventTable, attribute: .bottom, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0) 
    NSLayoutConstraint.activate([tempX, tempY]) 


    eventTable.delegate = self 
    eventTable.dataSource = self 

    eventTable.reloadData() 

    preferredContentSize.height = 200 


} 

回答

6

在iOS系統10設置preferredContentSize.height不直接iOS的10後工作

,有兩種類型,顯示今天的擴展

情況下1顯示更多(擴展型,可以設置自定義高度)

殼體2顯示更少(緊湊型,具有默認hegiht)

enter image description here

What's new in iOS 10

所以你應該爲iOS 10的早期版本,併爲iOS提供10高度

if #available(iOSApplicationExtension 10.0, *) { 
     //setup display mode (show more(.expended) or show less(.compact)) 
     extensionContext?.widgetLargestAvailableDisplayMode = .expanded 
    } else { 
     // Fallback on earlier versions 
     preferredContentSize.height = 200 
    } 

和shoulde實施自定義高度

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {

@available(iOSApplicationExtension 10.0, *) 
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) { 
    switch activeDisplayMode { 
    case .expanded: preferredContentSize.height = 200 
    case .compact: preferredContentSize = maxSize 
    } 
} 
相關問題