自動レイアウトは初めてです。xibファイルからすべてのプロジェクトを実行しましたが、プログラムでビューの高さを更新する必要があるという問題に直面しました。私は以下を試しましたが、現在は機能しています。
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:loginContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:loginFrame.size.height]];
コンソールではそれはショーです
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:0x78724530 V:[UIView:0x790cdfb0(170)]>",
"<NSLayoutConstraint:0x787da210 V:[UIView:0x790cdfb0(400)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x78724530 V:[UIView:0x790cdfb0(170)]>
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.
新しい制約を追加する代わりに、既存の制約の定数を変更する必要があります。
IBOutletを使用して、Interface Builderで制約に接続します。
@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;
次に、プログラムで設定する必要がある場合は、制約の定数プロパティを設定するだけです。
heightConstraint.constant = 100;
または
Interface Builderでnibにアクセスできない場合は、コードで制約を見つけます。
NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in myView.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
heightConstraint = constraint;
break;
}
}
heightConstraint.constant = 100;
そしてSwiftでは:
if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {
constraint.constant = 100.0
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加