开发者社区> 问答> 正文

今天小部件中的自定义视图相互堆叠,而不是一个叠加在另一个上

我有一个我搞不懂的问题。我需要构建一个小部件,我有两个自定义视图(现在使用相同的自定义视图)

问题是,由于某种原因,它们相互叠加(我可以看到,在第一次打印中,我在子视图上,在第二次打印中,我有两个子视图,并且只在包含视图的顶部看到了第二个子视图约束)。

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
    }
}

知道为什么吗?

展开
收起
游客5akardh5cojhg 2019-12-10 22:51:08 568 0
1 条回答
写回答
取消 提交回答
  • 你可能需要

    currentView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
    
    2019-12-10 22:51:24
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载