今天小部件中的自定义视图相互堆叠而不是一个在另一个下面
Custom Views in today widget stack on each other instead of one under the other
我遇到了无法解决的问题。
我需要构建一个小部件并且我有两个自定义视图(现在使用相同的自定义视图)
问题是由于某种原因它们相互堆叠(我可以看到在第一个打印中我有子视图,在第二个打印中我有两个并且只看到第二个子视图约束到顶部包含视图)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
guard let currentView = Bundle.main.loadNibNamed("CurrentView", owner: self, options: nil)?.first as? CurrentView else { return }
currentView.frame = self.view.frame
self.view.addSubview(currentView)
currentView.forcastImage.image = #imageLiteral(resourceName: "13")
currentView.translatesAutoresizingMaskIntoConstraints = false
currentView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
currentView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
currentView.heightAnchor.constraint(equalToConstant: 110).isActive = true
print(self.view.subviews.count)
if let hourlyView = Bundle.main.loadNibNamed("CurrentView", owner: self, options: nil)?.first as? CurrentView {
hourlyView.frame = self.view.frame
self.view.addSubview(hourlyView)
hourlyView.forcastImage.image = #imageLiteral(resourceName: "6")
hourlyView.translatesAutoresizingMaskIntoConstraints = false
hourlyView.topAnchor.constraint(equalTo: currentView.bottomAnchor, constant: 150).isActive = true
hourlyView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
hourlyView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
hourlyView.heightAnchor.constraint(equalToConstant: 110).isActive = true
}
print(self.view.subviews.count)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
preferredContentSize = CGSize(width: 0, height: 250)
} else {
preferredContentSize = maxSize
}
}
知道这是为什么吗?
您可能需要
currentView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
我遇到了无法解决的问题。 我需要构建一个小部件并且我有两个自定义视图(现在使用相同的自定义视图)
问题是由于某种原因它们相互堆叠(我可以看到在第一个打印中我有子视图,在第二个打印中我有两个并且只看到第二个子视图约束到顶部包含视图)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
guard let currentView = Bundle.main.loadNibNamed("CurrentView", owner: self, options: nil)?.first as? CurrentView else { return }
currentView.frame = self.view.frame
self.view.addSubview(currentView)
currentView.forcastImage.image = #imageLiteral(resourceName: "13")
currentView.translatesAutoresizingMaskIntoConstraints = false
currentView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
currentView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
currentView.heightAnchor.constraint(equalToConstant: 110).isActive = true
print(self.view.subviews.count)
if let hourlyView = Bundle.main.loadNibNamed("CurrentView", owner: self, options: nil)?.first as? CurrentView {
hourlyView.frame = self.view.frame
self.view.addSubview(hourlyView)
hourlyView.forcastImage.image = #imageLiteral(resourceName: "6")
hourlyView.translatesAutoresizingMaskIntoConstraints = false
hourlyView.topAnchor.constraint(equalTo: currentView.bottomAnchor, constant: 150).isActive = true
hourlyView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
hourlyView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
hourlyView.heightAnchor.constraint(equalToConstant: 110).isActive = true
}
print(self.view.subviews.count)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
preferredContentSize = CGSize(width: 0, height: 250)
} else {
preferredContentSize = maxSize
}
}
知道这是为什么吗?
您可能需要
currentView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true