【问题标题】:Position Child View Controller Inside Parent将子视图控制器定位在父视图中
【发布时间】:2020-06-12 22:22:35
【问题描述】:

我试图在父视图控制器中放置一个子视图控制器,但是我总是收到错误...

由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚激活约束,因为它们没有共同的祖先。约束或其锚点是否引用不同视图层次结构中的项目?这是非法的。'

有人可以帮帮我吗?我不知道发生了什么。

父视图控制器

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

【问题讨论】:

  • 为 QuickFact() 创建一个对象并使用它代替 QuickFact()
  • 感谢您的想法,但我觉得使用父视图控制器和子视图控制器会更容易。你知道为什么我不能定位子视图控制器吗?
  • 为你添加了答案。

标签: swift xcode layout


【解决方案1】:

您收到此错误是因为您将fact 添加为子视图而不是header,因此由于header 不在视图层次结构中,因此它与存在的其他视图之间不能有约束 在视图层次结构中:

view.addSubview(fact)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

将其更改为:

view.addSubview(header)
[...]
header.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

另外,每次你说 QuickFact() 时,你都在创建一个全新的 QuickFact 视图控制器实例。您需要调用此初始化程序一次 并使用生成的实例来做任何您想做的事情。如果你不这样做,你会得到和以前一样的错误,但是在这一行:

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
}

【讨论】:

  • 非常感谢您的帮助。我为 QuickFact 创建了一个变量并且该变量有效,但即使我用标题替换了 fact ,我也没有出现错误,但 QuickFact 视图控制器没有显示。
  • 尝试更改 QuickFact 视图控制器视图的背景颜色,例如 quickFactVC.view.backgroundColor = .red 以查看它是否存在但不可见。您还可以使用视图调试器来查看您的快速事实视图是否存在。让我知道你发现了什么,以便我在必要时更新我的​​答案!
  • 是的,我更改了背景颜色,但视图控制器没有显示。
  • 是的,在我发表评论后,我注意到您已经在更改 setupBackgroundColorAndFactString 中的背景颜色,所以不要介意。嗯,不知道为什么它不会显示。试试我猜的 Debug View Hierarchy 按钮?
  • 非常感谢您的帮助。我不知道 Debug View Hierarchy 并且知道我最喜欢的 Xcode 工具。问题是标题隐藏在导航栏后面,而事实是文本视图。我不知道文本视图有自己的背景颜色,默认设置为白色。然而,我的文字颜色也是白色的,因此我看不到它。再次感谢。
【解决方案2】:

正如我之前在评论中所说。确保为QuickFact() 创建一个对象并使用它而不是QuickFact()

同样在setupAndPositionHeader 中将view.addSubview(fact) 替换为view.addSubview(header) 以添加header

完成上述更改后,我可以在示例项目中看到子视图。

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多