gpt4 book ai didi

iphone - 我怎样才能使 View 自动调整大小以适应 super View

转载 作者:行者123 更新时间:2023-12-03 20:28:25 25 4
gpt4 key购买 nike

我有 2 次浏览

首先是view1大小为100x100

第二个是 view2 大小为 200 x200

我的代码是

[view1 addSubview:view2];

但是view2的大小大于view1

如何使 view2 更小以适应 view1 的大小

谢谢

最佳答案

实现这一目标的现代方法是使用自动布局。这样,无论您的 View 如何旋转或变化,它们都会保持相同的大小。

根据您的设置,它会是这样的

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeWidth multiplier:1.f constant:0.f];

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeHeight multiplier:1.f constant:0.f];

[view1 addConstraints:@[widthConstraint,heightConstraint]];

如果您需要定位较早的操作系统,请将帧设置为相等并使用 autoresizingMask

关于iphone - 我怎样才能使 View 自动调整大小以适应 super View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378791/

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