将实例化的UIView从NIB约束到其他UIView

时间:2015-11-05 16:40:25

标签: ios iphone swift cocoa-touch uiview

我在将新实例化的UIView从NIB添加到另一个UIView时遇到了问题,另一个UIView是另一个NIB的一部分。

我想要做的就是:

  1. 从NIB文件加载UIView。 (完成)
  2. 在第一个NIB文件中有一个UIView组件,它充当另一个UIView的容器。灰色背景。 (完成)
  3. 从另一个NIB文件加载第二个UIView(绿色背景)并将其放在第一个UIView(灰色的)中。 (这就是问题所在)我的意思是里面正好在容器的位置。容器UIView是300x280(灰色背景),外部UIView(绿色背景)也是如此。
  4. 我可以在第一个UIView中添加第二个UIView,因为我可以看到它,但约束是关闭的。第一个UIView使用AutoLayout来容纳自己,但第二个UIView使用IB布局。

    观察:

    如果我将第二个UIView添加到容器UIView时添加背景颜色,那么框架非常合适,但组件不合适,它们似乎总是关闭。我已经尝试了clipToBounds = true,它的工作原理是将组件保留在UIView中,但我找不到将所有组件(UITextFields,UIButtons等)与两个UIViews对齐的方法。

    截图:

    enter image description here

    容器代码/第一个UIView:

    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
    
        // loginView  = IS THE UIView in the second NIB.        
        // otherViews = IS THE UIView CONTAINER in the first NIB.
    
        let loginView = BCAKLoginView() // LOAD NIB FILE
        self.loginView.frame  = self.otherViews.bounds
        self.loginView.hidden = false
        self.loginView.backgroundColor = UIColor.redColor()
        self.loginView.clipsToBounds = true
    
        self.otherViews.addSubview(loginView) // HERE I ADD THE SECOND NIB TO UIView.
    
        let constraintLeading = NSLayoutConstraint.init(
            item: self.loginView,
            attribute: NSLayoutAttribute.Leading,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.otherViews,
            attribute: NSLayoutAttribute.Leading,
            multiplier: 1.0,
            constant: 0.0
        )
        let constraintWidth = NSLayoutConstraint.init(
            item: self.loginView,
            attribute: NSLayoutAttribute.Width,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.otherViews,
            attribute: NSLayoutAttribute.Width,
            multiplier: 1.0,
            constant: 0.0
        )
        let constraintTop = NSLayoutConstraint.init(
            item: self.loginView,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.otherViews,
            attribute: NSLayoutAttribute.Top,
            multiplier: 1.0,
            constant: 0.0
        )
        let constraintBottom = NSLayoutConstraint.init(
            item: self.loginView,
            attribute: NSLayoutAttribute.Bottom,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.otherViews,
            attribute: NSLayoutAttribute.Bottom,
            multiplier: 1.0,
            constant: 0.0
        )
    
        self.otherViews.addConstraint(constraintLeading)
        self.otherViews.addConstraint(constraintWidth)
        self.otherViews.addConstraint(constraintTop)
        self.otherViews.addConstraint(constraintBottom)
    }
    

1 个答案:

答案 0 :(得分:0)

好吧,回答我自己的问题..

问题是当我实例化第二个NIB文件(UIView)时。在initWithFrame:initWithCoder:。我将它作为子视图添加到自己,因此在loginViewotherViews之间添加了额外的UIView。就是这样......当我删除这个额外的视图时,一切都是自我调整的。