iOS - 为动态创建的视图设置适当的约束

时间:2014-09-08 11:11:12

标签: ios iphone swift constraints

我有这样的想法(抱歉这个丑陋的代码只是为了测试):

override func viewDidLoad() {
    super.viewDidLoad()
    self.emptyScrollView.scrollEnabled = true
    var emptyView = UIView()
    self.emptyScrollView.addSubview(emptyView)
    var frame2 = CGRect()
    frame2 = emptyView.frame
    frame2.origin.x = 0
    frame2.origin.y = 10
    frame2.size.width = self.view.frame.width
    emptyView.frame = frame2
    var textView = UITextView()
    textView.text = sampleText
    var frame = CGRect()
    frame = emptyView.frame
    textView.frame = frame
    textView.font = UIFont(name: "Helvetica Neue", size: 14)
    textView.textColor = UIColor.blackColor()
    textView.backgroundColor = UIColor.redColor()
    emptyView.addSubview(textView)
    textView.sizeToFit()
    textView.layoutIfNeeded()

    var bootomY = CGRectGetMaxY(textView.frame)
    var emptyView2 = UIView()
    self.emptyScrollView.addSubview(emptyView2)
    var frame3 = CGRect()
    frame3 = emptyView2.frame
    frame3.origin.x = 0
    frame3.origin.y = bootomY
    frame3.size.width = self.view.frame.width
    emptyView2.frame = frame3
    var textView2 = UITextView()
    textView2.text = sampleText2
    var frame4 = CGRect()
    frame4 = emptyView2.frame
    textView2.frame = frame4
    textView2.font = UIFont(name: "Helvetica Neue", size: 14)
    textView2.textColor = UIColor.blackColor()
    emptyView2.addSubview(textView2)
    textView2.sizeToFit()
    textView2.layoutIfNeeded()

    var scrollHeight = textView.frame.height + textView2.frame.height + 1000
    self.emptyScrollView.contentSize = CGSize(width: self.view.frame.width, height: scrollHeight)

    var constraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: emptyView2, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 10)
    self.emptyScrollView.addConstraint(constraint)
}

当我运行app时,我遇到了错误:

014-09-08 13:05:20.323 CutsomViewWithText[9123:3532873] Unable to simultaneously satisfy 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:0x7ba39240 UIView:0x7a758e30.bottom == UIView:0x7a76d3e0.top + 10>",
"<NSAutoresizingMaskLayoutConstraint:0x7a74c470 h=--& v=--& UIView:0x7a758e30.midY == + 10>",
"<NSAutoresizingMaskLayoutConstraint:0x7a75c480 h=--& v=--& V:[UIView:0x7a758e30(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7a76a990 h=--& v=--& UIView:0x7a76d3e0.midY == + 226.5>",
"<NSAutoresizingMaskLayoutConstraint:0x7a76a9c0 h=--& v=--& V:[UIView:0x7a76d3e0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ba39240 UIView:0x7a758e30.bottom == UIView:0x7a76d3e0.top + 10>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h>   may also be helpful.
2014-09-08 13:05:23.663 CutsomViewWithText[9123:3533398] Terminating since there is no system app.

你能帮帮我,我该怎么做才能在第一个视图和第二个视图之间设置适当的约束?

2 个答案:

答案 0 :(得分:1)

您需要添加以下代码行:

    emptyView.setTranslatesAutoresizingMaskIntoConstraints(false)
    emptyView2.setTranslatesAutoresizingMaskIntoConstraints(false)

    var constraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: emptyView2, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 10)

并且不要忘记用正确的框架初始化emptyScrollView,否则你将有一个黑屏

答案 1 :(得分:0)

要为视图添加的约束类型。

左,上,宽度和高度

左约束(样本):

myView1.setTranslatesAutoresizingMaskIntoConstraints(假)  myView2.setTranslatesAutoresizingMaskIntoConstraints(假)

self.addConstraint(NSLayoutConstraint(

                item:myView1, attribute:NSLayoutAttribute.Left,

                relatedBy:NSLayoutRelation.Equal, toItem:myView2,

                attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: 0.0))