6

自動レイアウトを有効にしてストーリーボードにレイアウトされたビュー コントローラーがあり、ビューを横向きに回転させて画面上のボタンを再配置できるように制約を変更する方法を探しています。以下のコードを試してみると、「制約を満たすことができない、制約を破っている...」というメッセージが約 20 個表示されますが、実際には解読できません。

ストーリーボードの制約をプログラムで指定した制約に動的に置き換える方法はありますか? ストーリーボードで定義したボタンのレイアウトを完全にオーバーライドしたいと考えています。

-(void)updateViewConstraints
{
    [super updateViewConstraints];

    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.getStartedButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.takeTourButton.translatesAutoresizingMaskIntoConstraints = NO;



    [self.loginButton removeConstraints:self.loginButton.constraints];
    [self.getStartedButton removeConstraints:self.getStartedButton.constraints];
    [self.takeTourButton removeConstraints:self.takeTourButton.constraints];


    one = self.loginButton;
    two = self.getStartedButton;
    three = self.takeTourButton;


    NSDictionary *metrics = @{@"height":@50.0,@"width":@100.0,@"leftSpacing":@110.0};
    NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

    [self.view removeConstraints:activeTestLabelConstraints];
    [activeTestLabelConstraints removeAllObjects];

    if(isRotatingToLandscape)
    {

        [self registerConstraintsWithVisualFormat:@"|-[one(two)]-[two(three)]-[three]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"V:[one(height)]-|" options:0 metrics:metrics views:views];

    }else
    {

        [self registerConstraintsWithVisualFormat:@"|-leftSpacing-[one(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"[two(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"[three(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"V:[one(height)]-[two(75)]-[three(100)]-|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:views];
    }


}

ロブの回答で更新されました。これが、私が使用する制約を削除する方法です

-(void)removeConstraintForView:(UIView*)viewToModify
{

    UIView* temp = viewToModify;
    [temp removeFromSuperview];
    [self.view addSubview:temp];

}
4

1 に答える 1

11

ビューのすべての制約を削除したいだけのようです。ビューの制約は多くの場合、ビューの祖先によって保持されているため、すべての制約を簡単に削除する方法は明らかではありません。しかし、実際にはかなり簡単です。

ビュー階層からビューを削除すると、そのビューとその外部の他のビューの間の制約がすべて削除されます。したがって、スーパービューからビューを削除してから、再度追加してください。

// Remove constraints betwixt someView and non-descendants of someView.
UIView *superview = someView.superview;
[someView removeFromSuperview];
[superview addSubview:someView];

someViewとその子孫の間に何らかの制約がある場合、それらの制約は単独で保持される可能性がありsomeViewます (ただし、 の子孫は保持できませんsomeView)。これらの制約も削除したい場合は、直接削除できます。

// Remove any remaining constraints betwixt someView and its descendants.
[someView removeConstraints:[someView constraints]];
于 2013-07-25T21:21:11.713 に答える