gpt4 book ai didi

ios - iOS 中的自动布局问题

转载 作者:行者123 更新时间:2023-11-29 03:42:04 24 4
gpt4 key购买 nike

我在 UIView 类型的容器 View 中有一个标签、图像和按钮。容器 View 又是 UIView 类型的 super 容器 View 的 subview 。所有布局都是使用 autlayout 视觉格式语言在代码中完成的。我想要实现的是按下按钮时删除标签并期望 super 容器调整其内容的大小。但目前发生的情况是整个 super 容器从屏幕上消失。有人能告诉我为什么会发生这种情况吗?附件是我的代码示例。

- (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];

}

还有一个问题。当相关 View 从其父 View 中删除时,约束对象会发生什么情况。它们是否存在或被删除?

最佳答案

您可以添加另一个约束,该约束应在没有 helpLabel 及其约束的情况下应用,但为这个新约束赋予较低的优先级,以便当 helpLabel目前,它的约束将控制 View ,但是当 helpLabel 被删除时,新的约束将发挥作用。例如:

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

顺便说一句,如果您想查看删除 helpLabel 后 View 发生了什么,请使用调试器运行应用程序,点击暂停按钮 ( pause ) 暂停应用程序,然后在 (lldb) 调试器提示符处键入以下命令:

po [[UIWindow keyWindow] recursiveDescription]

请注意,有时很难识别哪个窗口是哪个窗口,因此我会经常为各个 View 提供唯一的数字 tag 属性,以使其更容易。

此外,在同一命令行中,您可以使用以下命令来识别约束中不明确的布局:

po [[UIWindow keyWindow] _autolayoutTrace]

关于ios - iOS 中的自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288159/

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