我有2个意见
首先是view1大小为100x100
第二个是view2大小是200 x200
我的代码是
[view1 addSubview:view2];
但view2的大小大于view1
我怎样才能使view2变小以适合view1的大小
谢谢
最佳答案
实现此目的的现代方法是使用自动布局。这样,无论视图如何旋转或更改,它们都将保持不变。
根据您的设置,可能是这样的
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
。