【问题标题】:NSLayoutConstraint : resize view when neededNSLayoutConstraint :在需要时调整视图大小
【发布时间】:2014-11-28 20:03:58
【问题描述】:

我有 2 个“姐妹视图”,一个位于另一个上方(说话的 Y)嵌入在同一个父视图中。

第一个是屏幕大小,下面是屏幕底部的大小。
在动画中,底部视图会升高并显示在屏幕上。

这个动画应该减少上视图的高度。

我想为此使用NSLayoutConstraint,但我找不到工作。

现在我有以下内容:

let constraintString : String = String(format: "V:[tranlucentView][buttonContainer(%lg)]", buttonHeight * countOfButtons)

let viewsDict:NSDictionary = ["tranlucentView" : self.translucentView, "buttonContainer" : self.buttonContainer]
var constraints : NSArray = NSLayoutConstraint.constraintsWithVisualFormat(constraintString, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)
self.view.addConstraints(constraints)

这就是我得到的错误:

Probably at least one of the constraints in the following list is one you don't want. Try 

this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x198f6530 V:[UIView:0x198ef880]-(0)-[UIView:0x177b11e0]>",
    "<NSLayoutConstraint:0x198f6600 V:[UIView:0x177b11e0(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x19f7f7f0 h=--& v=--& UIView:0x198ef880.midY == + 284>",
    "<NSAutoresizingMaskLayoutConstraint:0x1983e4c0 h=--& v=--& V:[UIView:0x198ef880(568)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x19f7f260 h=--& v=--& UIView:0x177b11e0.midY == + 472>"
)

对我的问题有什么建议吗?

【问题讨论】:

    标签: cocoa swift nslayoutconstraint


    【解决方案1】:

    我自己找到了解决方案:

    我需要 translatesAutoresizingMaskIntoConstraintsfalse 让我的上视图允许调整其大小。

    我还需要为其宽度添加一个约束,否则它的宽度将变为 0。

    let constraintString : String = String(format: "V:|[tranlucentView][buttonContainer(%lg)]|", buttonHeight * countOfButtons)

    self.translucentView.setTranslatesAutoresizingMaskIntoConstraints(false)
    let viewsDict:NSDictionary = ["tranlucentView" : self.translucentView, "buttonContainer" : self.buttonContainer]
    
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(constraintString, options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
    
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[tranlucentView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 2012-04-15
      相关资源
      最近更新 更多