- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在父 View 中放置一个 subview Controller ,但是我总是收到错误...
由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用 anchor 激活约束,因为它们没有共同的祖先。约束或其 anchor 是否引用了不同 View 层次结构中的项?那是非法的。
有人可以帮帮我吗?我不知道发生了什么。
父 View Controller
class MainView: UIViewController {
let settingsIcon = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
addQuickFactView()
setupNavBar()
}
func setupNavBar() {
navigationItem.title = "Home"
navigationController?.navigationBar.barTintColor = .clear
settingsIcon.setImage(UIImage(named: "SettingsIcon"), for: .normal)
settingsIcon.addTarget(self, action: #selector(goToSettings), for: .touchUpInside)
settingsIcon.heightAnchor.constraint(equalToConstant: 50).isActive = true
settingsIcon.widthAnchor.constraint(equalTo: settingsIcon.heightAnchor).isActive = true
let leftButton = UIBarButtonItem(customView: settingsIcon)
self.navigationItem.leftBarButtonItem = leftButton
}
@objc func goToSettings() {
navigationController?.pushViewController(Settings(), animated: true)
}
func addQuickFactView() {
addChild(QuickFact())
view.addSubview(QuickFact().view)
QuickFact().didMove(toParent: self)
QuickFact().view.translatesAutoresizingMaskIntoConstraints = false
QuickFact().view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
QuickFact().view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
QuickFact().view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
QuickFact().view.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
}
class QuickFact: UIViewController {
var category = ""
var factString = ""
let header = UILabel()
let fact = UITextView()
let facts = [["Recycling cardboard only takes 75% of the energy required to make new cardboard.", "Over 90% of all products shipped in the U.S. are shipped in corrugated boxes, which makes up more than 400 billion square feet of cardboard.", "Around 80% of retailers and grocers recycle cardboard.", "70% of corrugated cardboard is recovered for recycling., Approximately 100 billion cardboard boxes are produced each year in the U.S.", "One ton of recycled cardboard saves 46 gallons of oil.", "One ton of recycled cardboard saves 9 cubic yards of landfill space."],
["Nearly half of the food in the U.S. goes to waste - approximately 3,000 pounds per second.","Only about 5% of food is diverted from landfill.","The U.S. produces approximately 34 million tons of food waste each year.","Food scraps make up almost 12% of municipal solid waste generated in the U.S.","In 2015, about 137.7 million tons of MSW were landfilled. Food was the largest component at about 22%."],
["2.5 million plastic bottles are thrown away every hour in America.","Recycling plastic takes 88% less energy than making it from raw materials.","Enough plastic is thrown away each year to circle the earth four times.","Only 23% of disposable water bottles are recycled.","Plastic bags can take up to 1,000 years to decompose.","Recycling one ton of plastic saves the equivalent of 1,000–2,000 gallons of gasoline.","Recycling plastic saves twice as much energy as burning it in an incinerator.","Styrofoam never decomposes.","The world produces more than 14 million tons of Polystyrene (plastic foam) each year.","Recycling one ton of plastic bottles saves the equivalent energy usage of a two person household for one year."],
["A modern glass bottle would take 4,000 years or more to decompose -- and even longer if it's in landfill.","Glass can be recycled and re-manufactured an infinite amount of times and never wear out.","More than 28 billion glass bottles and jars go to landfills every year. That's enough to fill two Empire State Buildings every three weeks.","A modern glass bottle would take 4,000 years or more to decompose − and even longer if it’s in landfill."],
["Americans use 85 million tons of paper per year which is about 680 pounds per person.","70% of the total waste in offices is paper waste.","Recycling one ton of paper saves 7,000 gallons of water.","The average office worker uses 10,000 sheets of paper per year.","American businesses use around 21 million tons of paper - with about 750,000 copies made every minute.","Each ton of recycled paper can save 17 mature trees.","Recycling a stack of newspaper just 3 feet high saves one tree.","Approximately 1 billion trees worth of paper are thrown away every year in the U.S."],
["The average person has the opportunity to recycle more than 25,000 cans in their life.","An aluminum can can be recycled and back on a grocery store shelf as a new can in as little as 60 days.","Aluminum can be recycled forever without any loss of quality.","Aluminum can be recycled using only 5% of the energy used to make the product from new.","Americans throw away 25 billion Styrofoam coffee cups every year.","Recycling a single aluminum can saves enough energy to power a TV for 3 hours."],
["About 11 million tons of textiles end up in U.S. landfills each year — an average of about 70 pounds per person.","In 2007, 1.8 million tons of e-waste ended up in landfills.","The average person generates 4.4 pounds of solid waste every day.","In 2014, The U.S. generated 258 million tons of municipal solid waste (MSW).","The EPA estimates that 75% of the American waste stream is recyclable, but we only recycle about 30% of it.","94% of the U.S. population has access to some type of recycling program.","Americans generate an additional 5 million tons of waste throughout the holidays.","Americans throw away enough trash in an average year to circle the earth 24 times.","Electronic waste totals approximately 2% of the waste stream in the U.S.","On average, it costs $30 per ton to recycle trash, $50 to send it to the landfill and $65 to $75 to incinerate it."]]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setupBackgroundColorAndFactString()
setupAndPositionHeader()
setupAndPositionFact()
}
func setupBackgroundColorAndFactString() {
let backgroundRandom = Int.random(in: 0...6)
let factRandom = Int.random(in: 0...facts[backgroundRandom].count - 1)
if backgroundRandom == 0 {
view.backgroundColor = .gray
category = "Cardboard"
factString = facts[0][factRandom]
}
if backgroundRandom == 1 {
view.backgroundColor = .green
category = "Organics"
factString = facts[1][factRandom]
}
if backgroundRandom == 2 {
view.backgroundColor = .blue
category = "Plastic"
factString = facts[2][factRandom]
}
if backgroundRandom == 3 {
view.backgroundColor = .cyan
category = "Glass"
factString = facts[3][factRandom]
}
if backgroundRandom == 4 {
view.backgroundColor = .white
category = "Paper"
factString = facts[4][factRandom]
}
if backgroundRandom == 5 {
view.backgroundColor = .lightGray
category = "Metal"
factString = facts[5][factRandom]
}
if backgroundRandom == 6 {
view.backgroundColor = .orange
category = "General"
factString = facts[6][factRandom]
}
}
func setupAndPositionHeader() {
header.text = "Did You Know..."
header.textColor = .white
header.textAlignment = .left
view.addSubview(fact)
header.translatesAutoresizingMaskIntoConstraints = false
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
header.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
header.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
header.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
func setupAndPositionFact() {
fact.text = category + "\n" + factString
fact.textColor = .white
fact.textAlignment = .left
fact.isScrollEnabled = false
view.addSubview(fact)
fact.translatesAutoresizingMaskIntoConstraints = false
fact.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
fact.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
fact.topAnchor.constraint(equalTo: header.bottomAnchor).isActive = true
fact.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
最佳答案
您收到此错误是因为您正在添加 fact
作为 subview 而不是 header
,所以自从 header
不在 View 层次结构中,它与 View 层次结构中的其他 View 之间不能有约束:
view.addSubview(fact)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
view.addSubview(header)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
QuickFact()
您正在创建一个全新的
QuickFact
实例 View Controller 。您需要调用此初始化程序一次并使用结果实例来执行您想要的任何操作。如果你不这样做,你会得到和以前一样的错误,但在这一行:
QuickFact().view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
addQuickFactView
功能如下:
func addQuickFactView() {
// Make a single instance of QuickFact and use it later on:
let quickFactVC = QuickFact()
addChild(quickFactVC)
view.addSubview(quickFactVC.view)
quickFactVC.didMove(toParent: self)
quickFactVC.view.translatesAutoresizingMaskIntoConstraints = false
quickFactVC.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
quickFactVC.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
quickFactVC.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
quickFactVC.view.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
关于swift - 将 subview Controller 定位在父级内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60458068/
我目前正在寻找一些关于 jQuery 的建议,因为我认为我做错了,即使我得到了我想要的结果。 我想在更改时将输入的值更改为最接近的具有 .milestone 类的输入的值。我想要更改的输入是保持输入,
我已经阅读有关绑定(bind)、调用、申请的文章近一周了,对我来说仍然很复杂。我想我写的这个 jsfiddle 需要它们。然而,我没能做到,因为我仍然很困惑。 我尽力写了一些我上周从遇到这个问题的开发
我有一个项目生成代码。生成时间真的很长,所以我把它分成了多个项目,每个项目产生了整体的 20%。原始 POM 成为“父 POM”,子项依赖于它,仅包含一个单独的 Artifact ID 和一两个更改的
我正在使用局部 View 来创建父 subview 。我最理想的是父 View 上的提交按钮,用于保存子值。 我有以下模型。 public class Course { public int
我刚刚开始学习Rust,并且在理解所有权如何在我的案例中遇到一些麻烦: use std::ops::IndexMut; // =====================================
我是 JavaScript 新手,想了解更多有关它实例化父/子对象的顺序的信息。更具体地说,我想从编译器/浏览器的 Angular 理解以下代码片段。 var parent = { child:
我正在测试 Azure IaaS,并遇到了一个非常基本的问题。我有一个父 VHD 和子 VHD,已使用 csupload 将其作为页面 blob 上传,并且门户中显示图像和磁盘。然后我尝试将 pare
我的应用程序会定期为我坚持使用的对象请求更新 Core Data到网络服务。然后我需要更新我在主要上下文中拥有的对象(默认情况下 AppDelegate 中提供的对象)。编辑对象的不是用户,所以我需要
texT text text text text text 如何直接获取来自.menu ? 里面的 child 不应该采取。
我一直需要影响与其他元素相关的元素,但我的方法有点业余! 即到 // matched item where script is called from LINK 我使用; $(thi
我有两个表: 父子“类别”: id name parent_id 1 Food NULL 2 Pizza 1 3 Pasta
Linux 上的 Python 2.7.6。 我正在使用从父级继承的测试类。父类保存了许多子类共有的许多字段,我需要调用父类的 setUp 方法来初始化这些字段。调用 ParentClass.setU
我有一个处理图像、相册和相册类别的数据库。 一个专辑可以有多个专辑(子专辑),并且只有 1 级深度。 一张专辑仅属于一个专辑类别。 在这里做了一些研究,我相信最合适的数据库模型是这个 album_ca
我有一个关键字表,其中每个关键字都分配有一个 ID,并且是唯一的。我有第二个表,将父关键字的 ID 链接到子关键字的 ID。一个关键字最多可以有大约 800 个 child 或根本没有。 child
我经常使用这个 CSS 选择器 parent>child。我的设计在 Mozilla 和 Opera 中看起来不错。 但在 IE 中,它很糟糕。我知道 > 在 IE 中无法识别,但在 IE 中有什么替
我一直在用一个父对象构建一个系统,它在其中创建各种子对象,每个子对象都需要一个主对象才能运行。现在,到目前为止,我一直在创建 shared_ptr和 Child* ,所以当 Parent 和 所有 C
我从以下两个类中收到序列化兼容性错误。只有父类CommericalCustomer 实现了序列化。当具有如下所示的父/子关系时,使用可序列化接口(interface)的正确方法是什么? public
我正在开发一个程序并学习父/子进程。目前我的子进程是 exit(variable); 在我的 main() 中我有: signal(SIGCHLD, chldHandler); 在我的 main()
考虑以下两个具体类: public class A { protected void foo() { System.out.println("A foo"); bar
所以,我正在尝试建立这样的父/子类关系: class ParentClass where C : ChildClass { public void AddChild(C child)
我是一名优秀的程序员,十分优秀!