這樣的描述說我正在一個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
}