简体   繁体   English

根据另一个UIViews高度动态更改情节提要中UIView的高度

[英]Dynamically change height of UIView in Storyboard based from another UIViews height

Considering the following image, object B needs be dynamic in height based off number of UILabels inside it. 考虑下图,对象B的高度需要根据其内部UILabel的数量而变化。 Object B heigh must then effect the height of object C. 然后,对象B的高度必须影响对象C的高度。

I currently have this implemented in storyboard however I am stuck on these two things, What should Object B be made of? 我目前已在情节提要中实现了此功能,但是我仍然牢记在这两件事上:对象B应该由什么组成? (I cannot use a stack view as my deployment target is 7.1) it needs to have repeating list (我不能使用堆栈视图,因为我的部署目标是7.1)它需要重复列表

[uiview] - [uilabel]

Object C is a UIView and I would like the height of Object B to effect its height? 对象C是UIView,我希望对象B的高度影响其高度吗? how can I achieve that? 我该如何实现?

在此处输入图片说明

You can use this type of code 您可以使用这种类型的代码

int lblY = 10; // As per your requirement
int viewBHT = 50; // As per your requirement

UILabel *lbl;

NSMutableArray *arrLbl = [[NSMutableArray alloc] initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1", nil];

UIView *viewB = [[UIView alloc] init];
[viewB setFrame:CGRectMake(0, viewA.frame.origin.y, self.view.frame.size.width, viewBHT)]; 
[viewB setBackgroundColor:[UIColor darkGrayColor]];
[self.view addSubview:viewB];

for (int i = 0; i < arrLbl.count; i++)
{
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, lblY , viewB.frame.size.width - 20, 30)];
    [lbl setText:[NSString stringWithFormat:@"Hello %d",i]];
    [lbl setBackgroundColor:[UIColor lightGrayColor]];
    [viewB addSubview:lbl];

    lblY = lblY + lbl.frame.size.height + 10;
}

viewBHT = lbl.frame.size.height + lbl.frame.origin.y + 10;
[viewB setFrame:CGRectMake(0, 80, self.view.frame.size.width, viewBHT)];

UIView *viewC = [[UIView alloc] init];
[viewC setFrame:CGRectMake(0, viewB.frame.origin.y + viewB.frame.size.height + 10, self.view.frame.size.width, 50)];
[viewC setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:viewC];

Hope this will help you..!! 希望这个能对您有所帮助..!! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM