Swift 以编程方式解决容器视图约束问题

Swift 以编程方式解决容器视图约束问题,swift,uiview,uiviewcontroller,swift3,constraints,Swift,Uiview,Uiviewcontroller,Swift3,Constraints,我试图在我的选项卡栏上方包含一个容器UIView,但是它似乎没有显示在我的视图上,因为我认为这是约束问题。我希望我的容器视图在视图中看起来如此 但是,我的容器根本没有显示在视图中。 这是我的密码: self.mapContainer.layer.cornerRadius = 8 self.mapContainer.backgroundColor = UIColor.cyan self.mapContainer.translatesAutoresizingMaskIntoConst

我试图在我的选项卡栏上方包含一个容器UIView,但是它似乎没有显示在我的视图上,因为我认为这是约束问题。我希望我的容器视图在视图中看起来如此

但是,我的容器根本没有显示在视图中。 这是我的密码:

self.mapContainer.layer.cornerRadius = 8
    self.mapContainer.backgroundColor = UIColor.cyan
    self.mapContainer.translatesAutoresizingMaskIntoConstraints = false
        self.mapContainer.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(self.mapContainer)

    //constraints of the map view
        let heightTabBar = self.tabBarController?.tabBar.frame.size.height
        self.mapContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant : (heightTabBar)! + 220).isActive = true
        self.mapContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        //self.topContainer.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
        self.mapContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.3).isActive = true
        //self.mapContainer.widthAnchor.constraint(equalTo: view.widthAnchor).isActive =  true
        self.mapContainer.leadingAnchor.constraint(equalTo: view.trailingAnchor, constant: 20).isActive = true
        self.mapContainer.trailingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40).isActive = true
我的容器没有显示在哪里 ?问题:

在视图外部的底部锚点处,以正值推送视图。这就是你看不到它的原因

解决方案:

而不是使用equalTo:view.bottomAnchor添加常量:heightTabBar!+220您应该使用self.mapContainer.bottomAnchor.constraintequalTo:view.bottomAnchor,常量:-heightTabBar!+220.isActive=在你的下锚处为真。您还应该使容器的前导锚点依赖于视图的前导锚点,与后导锚点相同。此外,还需要将常量的负值设置为尾随锚点,以便在右侧添加填充。请记住:如果你想从顶部和左侧推,那么加;如果你想从右侧和底部推,那么减

这将有助于您了解代码注释中的提示:

import UIKit

class ViewController: UIViewController {

  var mapContainer:UIView!

  override func loadView() {
    super.loadView()

    view.backgroundColor = .white

    self.mapContainer = UIView()
    self.mapContainer.layer.cornerRadius = 8
    self.mapContainer.backgroundColor = UIColor.cyan
    self.mapContainer.translatesAutoresizingMaskIntoConstraints = false
    self.mapContainer.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(self.mapContainer)


    //constraints of the map view
    let heightTabBar = self.tabBarController?.tabBar.frame.size.height
    // You need to subtract to push from the bottom of the view
    self.mapContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant : -(heightTabBar! + 220)).isActive = true
    self.mapContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    self.mapContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.3).isActive = true

    // Your leading anchor of map container should depend on the leading anchor of the view
    self.mapContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
    // Your trailingAnchor anchor of map container should depend on the trailingAnchor anchor of the view
    // Here you need to subtract to push from the right
    self.mapContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
  }
}
结果应类似于此:

iPhone5S模拟器上的结果:


非常感谢你。基于iphone 6s的解决方案,我能够按照我所希望的方式实现这一点,但它在iphone 5S上似乎不起作用。你知道为什么吗?是的,很高兴我能帮助你!我想我以后会看看原因可能是什么,但如果这个答案解决了这个问题,你可能应该用你当前的代码创建一个新问题,并解释你的新问题,你可以将它链接到评论中,在模拟器中测试iPhone 5s,我觉得不错。也许你可以在一个新问题中描述一下,与iPhone6@Aboogie相比,你对iPhone5S的期望是什么,哪些不适合你