gpt4 book ai didi

ios - 自动布局约束不会因爱或金钱而更新

转载 作者:行者123 更新时间:2023-11-29 01:51:13 26 4
gpt4 key购买 nike

我在自定义类中有一个方法可以调整和约束自定义 UIView:

-(void) sizeAndConstrainHeaderView:(HeaderView *)headerView inView:(UIView *)view {

CGFloat height;
// code to calculate height is omitted.

headerView.heightConstraint = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];

headerView.translatesAutoresizingMaskIntoConstraints = NO;

if (!headerView.isConstrained) {
[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:view.frame.size.width]];
[headerView addConstraint:headerView.heightConstraint];
headerView.isConstrained = YES;
}

headerView.heightConstraint.constant = height;
// trying everything here to get the constraint to update
[headerView setNeedsUpdateConstraints];
[headerView layoutIfNeeded];
[view setNeedsUpdateConstraints];
[view layoutIfNeeded];

NSLog(@"height constraint constant: %f", headerView.heightConstraint.constant);

}

我在 View Controller 中这样调用它:

[customClass sizeAndConstrainHeaderView:_headerView inView:self.view];

由于它被调用了多次(一次在检索数据之前,一次在检索数据之后),所以有一个标志用于是否添加约束。然后,无论如何,将高度常数设置为计算出的高度。但是无论我怎么调用,在 View 上还是自定义 View 上, View 的高度都不会改变,我也想不通为什么。其他约束工作正常。日志显示高度已更改。但是 View 没有反射(reflect)它。我肯定知道,因为我调试了 View 层次结构并且可以看到它没有更新。

最佳答案

你说

Since this gets called several times (once before data is retrieved, once after), there is a flag for whether to add constraints.

但是您的方法会在每次 调用时执行此操作:

headerView.heightConstraint = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];

因此在每次调用时,您都会创建一个新约束,将其存储在 headerView.heightConstraint 中,并设置新约束的常量。

但是,您只能在第一次调用该方法时安装此新约束。

也许您应该只在第一次调用时创建约束:

-(void) sizeAndConstrainHeaderView:(HeaderView *)headerView inView:(UIView *)view {

CGFloat height;
// code to calculate height is omitted.

if (!headerView.isConstrained) {
headerView.translatesAutoresizingMaskIntoConstraints = NO;

[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:view.frame.size.width]];

headerView.heightConstraint = [NSLayoutConstraint constraintWithItem:headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];

[headerView addConstraint:headerView.heightConstraint];
headerView.isConstrained = YES;
}

headerView.heightConstraint.constant = height;
}

关于ios - 自动布局约束不会因爱或金钱而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31389518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com