spa0201su
spa0201su

Reputation: 125

I want my three textViews have the same size

I see this error, when I click the 'aaa view' on the simulator. 'mySecondCustomView' is the container view of them.

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.

my code is below.

aaa.topAnchor.constraint(equalTo: mySecondCustomView.topAnchor, constant: 10).isActive = true
aaa.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
aaa.trailingAnchor.constraint(equalTo: mySecondCustomView.trailingAnchor, constant: -10).isActive = true
aaa.bottomAnchor.constraint(equalTo: bbb.topAnchor, constant: -10).isActive = true
aaa.widthAnchor.constraint(equalTo: bbb.widthAnchor, constant: 0).isActive = true
aaa.heightAnchor.constraint(equalTo: bbb.heightAnchor, constant: 0).isActive = true

bbb.topAnchor.constraint(equalTo:aaa.bottomAnchor, constant:10).isActive = true
bbb.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
bbb.widthAnchor.constraint(equalTo: aaa.widthAnchor, multiplier: 1).isActive = true
bbb.heightAnchor.constraint(equalTo: aaa.heightAnchor, multiplier: 1).isActive = true

ccc.topAnchor.constraint(equalTo:bbb.bottomAnchor, constant:10).isActive = true
ccc.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
ccc.widthAnchor.constraint(equalTo: bbb.widthAnchor, multiplier: 1).isActive = true
ccc.heightAnchor.constraint(equalTo: bbb.heightAnchor, multiplier: 0.2).isActive = true

Upvotes: 0

Views: 33

Answers (1)

mhoeller
mhoeller

Reputation: 620

Your constraints are over-defined. For example in aaa You define both top/bottom + height and leading/trailing + width. At the same time you contrain aaa to be the same width as bbb and bbb to be the same width as aaa. These are unnecessary duplicate constraints.

Remove those lines and it should work:

aaa.widthAnchor.constraint(equalTo: bbb.widthAnchor, constant: 0).isActive = true
aaa.heightAnchor.constraint(equalTo: bbb.heightAnchor, constant: 0).isActive = true

EDIT:

I played around with this and think I know what your problem is. You probably still have an autoresizing mask. Try this:

aaa.translatesAutoresizingMaskIntoConstraints = false
bbb.translatesAutoresizingMaskIntoConstraints = false
ccc.translatesAutoresizingMaskIntoConstraints = false

aaa.topAnchor.constraint(equalTo: mySecondCustomView.topAnchor, constant: 10).isActive = true
aaa.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
aaa.trailingAnchor.constraint(equalTo: mySecondCustomView.trailingAnchor, constant: -10).isActive = true
aaa.heightAnchor.constraint(equalToConstant: 50).isActive = true

bbb.topAnchor.constraint(equalTo:aaa.bottomAnchor, constant:10).isActive = true
bbb.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
bbb.widthAnchor.constraint(equalTo: aaa.widthAnchor, multiplier: 1).isActive = true
bbb.heightAnchor.constraint(equalTo: aaa.heightAnchor, multiplier: 1).isActive = true

ccc.topAnchor.constraint(equalTo:bbb.bottomAnchor, constant:10).isActive = true
ccc.leadingAnchor.constraint(equalTo: mySecondCustomView.leadingAnchor, constant: 10).isActive = true
ccc.widthAnchor.constraint(equalTo: bbb.widthAnchor, multiplier: 1).isActive = true
ccc.heightAnchor.constraint(equalTo: bbb.heightAnchor, multiplier: 0.2).isActive = true

This produces this: enter image description here

Upvotes: 1

Related Questions