我有一个我搞不懂的问题。我需要构建一个小部件,我有两个自定义视图(现在使用相同的自定义视图)
问题是,由于某种原因,它们相互叠加(我可以看到,在第一次打印中,我在子视图上,在第二次打印中,我有两个子视图,并且只在包含视图的顶部看到了第二个子视图约束)。
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。