0

タイプ UIView のコンテナビューにラベル、画像、およびボタンがあります。コンテナー ビューは、再び UIView タイプのスーパーコンテナー ビューのサブビューです。すべてのレイアウトは、autolayout ビジュアル フォーマット言語を使用してコードで行われます。私が達成したいのは、ボタンが押されたときにラベルを削除し、スーパーコンテナがその内容のサイズを変更することを期待することです。しかし、現在起こっていることは、スーパー コンテナ全体が画面から消えることです。なぜこれが起こっているのか誰か教えてもらえますか?添付は私のコードサンプルです。

- (void)viewDidLoad {
    [super viewDidLoad];


    superContainer = [[UIView alloc] initWithFrame:CGRectZero];
    superContainer.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:superContainer];


    Container = [[UIView alloc] initWithFrame:CGRectZero];
    Container.backgroundColor = [UIColor redColor];

    [superContainer addSubview:Container];

    NSDictionary *sViews = NSDictionaryOfVariableBindings(Container);

    [superContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10.0-[Container]-10.0-|" options:0 metrics:nil views:sViews]];

    [superContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10.0-[Container]-10.0-|" options:0 metrics:nil views:sViews]];


    CGSize temp1 = [superContainer systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];


    superContainer.frame = CGRectMake(superContainer.frame.origin.x, superContainer.frame.origin.y, temp1.width, temp1.height);


    closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //[closeButton setImage: forState:UIControlStateNormal];
    [closeButton setBackgroundImage:[UIImage imageNamed:@"closebox.png"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"Close button frame is %@",NSStringFromCGRect(closeButton.frame));
    //closeButton.frame = CGRectMake(0, 10, 32, 32);
    [Container addSubview:closeButton];


    helpLabel = [[UILabel alloc] init];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text.This is sample Text. This is sample Text. "];
    helpLabel.attributedText = attrString;


    helpLabel.numberOfLines = 0;
    helpLabel.backgroundColor = [UIColor greenColor];
    [Container addSubview:helpLabel];


    helpImageView = [[UIImageView alloc] init];
    helpImageView.image = [UIImage imageNamed:@"testimage.png"];
    NSLog(@"frame of imageview is %@",NSStringFromCGRect(helpImageView.frame));
    [Container addSubview:helpImageView];

    dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dismissButton setTitle:@"Dismiss" forState:UIControlStateNormal];
    [[dismissButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
    dismissButton.backgroundColor = [UIColor blueColor];
    [Container addSubview:dismissButton];

    [Container setClipsToBounds:YES];

    [self addAutoLayoutProperties];

    NSDictionary *views = NSDictionaryOfVariableBindings(helpLabel,helpImageView,dismissButton,closeButton);

    NSDictionary *metrics = @{@"buttonHeight":@32.0};

    // Horizontal layout - for helplabel
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5.0-[helpLabel(400)]-5.0-|" options:NSLayoutFormatAlignAllLeft|NSLayoutFormatAlignAllRight metrics:metrics views:views]];
    // Horizontal layout - for helpImageView
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[helpImageView]|" options:NSLayoutFormatAlignAllLeft|NSLayoutFormatAlignAllRight metrics:metrics views:views]];
    // Horizontal layout - for dismissButton
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[dismissButton]-|" options:NSLayoutFormatAlignAllCenterX|NSLayoutFormatAlignAllCenterY metrics:metrics views:views]];
    // Horizontal layout - for dismissButton
    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[closeButton]-1.0-|" options:0 metrics:metrics views:views]];

    // Vertical layout

    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-1.0-[closeButton]" options:0 metrics:metrics views:views]];

    [Container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-buttonHeight-[helpLabel]-5.0-[helpImageView]-5.0-[dismissButton]-5.0-|" options:0 metrics:metrics views:views]];

    CGSize temp = [Container systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];


    Container.frame = CGRectMake(Container.frame.origin.x, Container.frame.origin.y, temp.width, temp.height);

    superContainer.center = self.view.center;

}

自動レイアウト プロパティを追加する私の方法

-(void)addAutoLayoutProperties {
    helpLabel.translatesAutoresizingMaskIntoConstraints = NO;
    helpImageView.translatesAutoresizingMaskIntoConstraints = NO;
    dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
    closeButton.translatesAutoresizingMaskIntoConstraints = NO;

    superContainer.translatesAutoresizingMaskIntoConstraints = NO;
    Container.translatesAutoresizingMaskIntoConstraints = NO;
}

ラベルを削除する私の方法。

- (IBAction)removeASubview:(id)sender {

    [helpLabel removeFromSuperview];

}

また、もう1つ質問です。関連するビューがそのスーパービューから削除されると、制約オブジェクトはどうなりますか。それらはそこに存在しますか、それとも削除されますか??

4

1 に答える 1

1

とその制約がない場合に適用する必要がある別の制約を追加できますhelpLabelが、この新しい制約の優先度を低くして、helpLabelが存在する場合はその制約がビューを支配し、helpLabelが削除されると新しい制約が来るようにします。遊びに。例えば:

[container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-buttonHeight@500-[helpImageView]" options:0 metrics:metrics views:views]];

ところで、helpLabel削除後にビューがどうなったかを確認したい場合は、デバッガーでアプリを実行し、一時停止ボタン ( 一時停止) をタップしてアプリを一時停止し、(lldb)デバッガー プロンプトで次のコマンドを入力します。

po [[UIWindow keyWindow] recursiveDescription]

どのウィンドウがどれであるかを特定するのが難しい場合があることに注意してください。そのためtag、簡単にするために、さまざまなビューに一意の数値プロパティを頻繁に指定します。

また、同じコマンド ラインで次のコマンドを使用して、制約内のあいまいなレイアウトを特定できます。

po [[UIWindow keyWindow] _autolayoutTrace]
于 2013-08-17T14:54:34.627 に答える