LakaLe_
LakaLe_

Reputation: 454

iOS - Set proper constraint for dynamiclly created views

I've got somethink like this (sorry for ugly code by this is just for test):

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)
}

And when I run app, I've got a error:

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.

Could you help me, what should I do to set proper constraint between first view and second view?

Upvotes: 0

Views: 377

Answers (2)

Ramesh
Ramesh

Reputation: 617

Type of constrains to be added for view.

left, top, width and height

left constrain(sample):

myView1.setTranslatesAutoresizingMaskIntoConstraints(false) myView2.setTranslatesAutoresizingMaskIntoConstraints(false)

self.addConstraint(NSLayoutConstraint(

                item:myView1, attribute:NSLayoutAttribute.Left,

                relatedBy:NSLayoutRelation.Equal, toItem:myView2,

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

Upvotes: 0

bogdan
bogdan

Reputation: 61

You need to add the following lines of code:

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

before

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

and don't forget to initialize emptyScrollView with a proper frame, otherwise you will have a black screen

Upvotes: 1

Related Questions