[英]Resize a UIView
我有兩個UIView
,比如containerView
和testView
。 說,的寬度testView
比的更大containerView
。 在這種情況下,當我將testView
作為子視圖添加到containerView
,出現了我的問題。 testView
的框架超出了containerView
的范圍。 這是我的代碼 -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
containerView.backgroundColor = [UIColor grayColor];
containerView.clipsToBounds = NO;
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
testView.backgroundColor = [UIColor yellowColor];
testView.layer.borderWidth = 3;
testView.layer.borderColor = [[UIColor blackColor] CGColor];
testView.center = CGPointMake(containerView.frame.size.width / 2, containerView.frame.size.height / 2);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 230, 50)];
label.text = @"This is a Label to test the size";
[testView addSubview:label];
[containerView addSubview:testView];
[self.view addSubview:containerView];
}
輸出到此代碼是 -
當containerView.clipsToBounds = YES;
,輸出是 -
但我真正需要的是 - (已編輯圖片)
當添加一個testView
其寬度/高度比其更大superview- containerView
,所述的幀testView
(包括兒童)應被調整,使得,它完全適合它的父內部。
我歡迎你的想法..!
執行此操作的現代方法是創建視圖,向視圖添加約束以建立與包含視圖的關系,然后將該視圖添加為子視圖。
這樣你就不會搞亂框架,並且會為你處理旋轉大小。
編輯添加
下面是一些示例代碼,可以執行類似於您要求的操作
- (void)viewDidLoad {
[super viewDidLoad];
// Create the containing view
UIView *greyView = [[UIView alloc] initWithFrame:CGRectZero];
greyView.backgroundColor = [UIColor grayColor];
greyView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:greyView];
// Create the label to go into the containing view
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"Example Text";
label.backgroundColor = [UIColor yellowColor];
[greyView addSubview:label];
NSDictionary *bindings = NSDictionaryOfVariableBindings(greyView, label);
// Set the constraints for the greyView relative to it's superview - to cover completely
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
// Set the constraints for the label to be pinned equally with padding
[greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
[greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
}
如果你想看到它在行動並自己調整約束,我已經上傳了一個你可以使用的簡單示例項目 。
為什么你將testView(黃色)的寬度設置得比它的容器大?然后抱怨它更大? 你應該這樣做
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
[...]
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 100)]; // 180 not 260
[...]
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 150, 50)]; // 150 not 230
或者使用建議的自動布局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.