私は Autolayout を学ぼうとしているので、チュートリアルを読んだり、いくつかの UIView をいじったりして、何ができるかを確認しています。自動レイアウトでポートレートからランドスケープに移行する際に、下の図のようなことをどのように達成するのだろうか? たとえば、黄色をビューの上部に固定し、青色を黄色に固定し、オレンジを青色に固定し、オレンジを下部に固定した場合、それらは比例してサイズが変更され、互いの間の 20 ピクセルが維持されると考えました。ビューの上部と下部。しかし、たとえば、青いボックスは、その上にある黄色のビューに固定されていても、ビューの上部にあるストラットを削除できません。高さと幅を均等に固定してサイズを変更できることは知っていますが、
2437 次
2 に答える
2
Interface Builder で制約を作成するのはイライラする可能性があり、Interface Builder はまだ などの乗数を使用して制約を作成できませんblue.height = red.height * 0.5
。ただし、それらはすべてコードで簡単に作成できます。
Interface Builder を使用して UIView の作成と色付けを行ったので、最初に Interface Builder が作成したすべての制約を削除します。
// in UIViewController.m
[self.view removeConstraints:self.view.constraints] ;
を使用して多くの制約をconstraintsWithVisualFormat:options:metrics:views:
作成するので、5 つの UIView へのポインターの辞書を作成し、制約を保持するために NSMutableArray を作成します。
NSDictionary* views = NSDictionaryOfVariableBindings(black, red, yellow, blue, orange) ;
NSMutableArray* constraints = [NSMutableArray new] ;
次に、UIView を配置し、同じ幅と高さを持つビューを示す制約を作成します。
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[black]-[yellow]-|" options:0 metrics:nil views:views]] ;
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[red(==black)]-[blue(==yellow)]-|" options:0 metrics:nil views:views]] ;
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[orange]-|" options:0 metrics:nil views:views]] ;
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[black]-[red(==black)]-[orange]-|" options:0 metrics:nil views:views]] ;
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[yellow]-[blue]-[orange]-|" options:0 metrics:nil views:views]] ;
最後に、乗数を使用して制約を作成し、すべての制約を追加します。
[constraints addObject:[NSLayoutConstraint constraintWithItem:orange attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:black attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]] ;
[constraints addObject:[NSLayoutConstraint constraintWithItem:yellow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:black attribute:NSLayoutAttributeHeight multiplier:1.5 constant:0]] ;
[constraints addObject:[NSLayoutConstraint constraintWithItem:yellow attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:black attribute:NSLayoutAttributeWidth multiplier:1.5 constant:0]] ;
[self.view addConstraints:constraints] ;
于 2013-05-07T16:59:45.173 に答える