我有这样的想法(对不起,丑陋的代码仅用于测试):
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)
}
当我运行应用程序时,出现错误:
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.
您能帮我吗,我该怎么做才能在第一视图和第二视图之间设置适当的约束?
最佳答案
您需要添加以下代码行:
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,否则您将出现黑屏