私の目標は、2つのラベルを重ねて配置することです。私はストーリーボード、labelAにラベルをドラッグし、それを上からランダムに固定しました。そして、私はラベルAをビューの中に水平に置いて、青の制約をもたらしました。IBを使って1つのラベルの制約を追加しました.1つのラベルの制約はプログラムによって追加されました
次に、2番目のラベルlabelBをストーリーボードにドラッグし、labelAの下に移動しました(たとえば、2つのラベルを区切る1インチ)。今、私はプログラム的labelAの上にそれを移動するlabelBの制約を設定しようとしています:私は私のアプリを実行したときに
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelA: UILabel!
@IBOutlet weak var labelB: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
labelB.translatesAutoresizingMaskIntoConstraints = false
//labelA.translatesAutoresizingMaskIntoConstraints = false
labelB.topAnchor.constraintEqualToAnchor(
labelA.topAnchor
).active = true
labelB.centerXAnchor.constraintEqualToAnchor(
view.centerXAnchor
).active = true
}
}
ただし、iOSでは制約の一部を壊し、そして私が見ることから等距離に2つのラベルであります上が、横にお互いに隣り合っています。さらに、labelBはlabelAをy位置まで引き下げます。私が見たいのは、labelAはlabelAの上にlabelBを付けて、私が最初にそれを拘束した場所に留まります。
IBを使用して1つのサブビューの制約を設定し、コードを使用して別のサブビューの制約を設定することはできますか?
出力:
2016-06-19 16:42:42.804 Test4[40792:232886] 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.
(
"<_UILayoutSupportConstraint:0x7fe8d2c13b20 V:[_UILayoutGuide:0x7fe8d2c1caa0(0)]>",
"<_UILayoutSupportConstraint:0x7fe8d2c245a0 V:|-(0)-[_UILayoutGuide:0x7fe8d2c1caa0] (Names: '|':UIView:0x7fe8d2c15790)>",
"<NSLayoutConstraint:0x7fe8d2c253e0 V:[_UILayoutGuide:0x7fe8d2c1caa0]-(111)-[UILabel:0x7fe8d2f169a0'LabelA']>",
"<NSLayoutConstraint:0x7fe8d2c26e70 UILabel:0x7fe8d2c22a50'LabelB'.top == UILabel:0x7fe8d2f169a0'LabelA'.top>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c25a20 'IB auto generated at build time for view with fixed frame' V:|-(256)-[UILabel:0x7fe8d2c22a50'LabelB'] (Names: '|':UIView:0x7fe8d2c15790)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8d2c253e0 V:[_UILayoutGuide:0x7fe8d2c1caa0]-(111)-[UILabel:0x7fe8d2f169a0'LabelA']>
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.
2016-06-19 16:42:42.807 Test4[40792:232886] 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.
(
"<NSLayoutConstraint:0x7fe8d2e24ba0 UILabel:0x7fe8d2c22a50'LabelB'.centerX == UIView:0x7fe8d2c15790.centerX>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c254d0 'IB auto generated at build time for view with fixed frame' H:|-(288)-[UILabel:0x7fe8d2c22a50'LabelB'](LTR) (Names: '|':UIView:0x7fe8d2c15790)>",
"<NSIBPrototypingLayoutConstraint:0x7fe8d2c25a70 'IB auto generated at build time for view with fixed frame' H:[UILabel:0x7fe8d2c22a50'LabelB'(52.5)]>",
"<NSLayoutConstraint:0x7fe8d2d8b020 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fe8d2c15790(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8d2e24ba0 UILabel:0x7fe8d2c22a50'LabelB'.centerX == UIView:0x7fe8d2c15790.centerX>
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.