[英]How to add View below other view in ViewController in iOS?
在我的应用程序中,我正在创建混合了UILabel
和UITextview
视图控制器,它们希望是可滚动的,因为文本是动态的并且也超过了垂直屏幕尺寸。
我目前有 Scrollview,它有如下子视图。 视图是在Xcode 4.3 Storyboard中创建的。
UILabel1(说标题)
UITextView1(可以是任意大小的动态文本)
UILabel2(第二个标题)
UITextView2(可以是任意大小的动态文本)
等等。
问题是
当UITextView1
有更多内容时,它会与我不想要的UILabel2
重叠。
我希望UILabel1
位于 UILabel1 下方的UILabel1
和UITextView1
UILabel1
。 UILabel2
下面UITextView1
等。
我必须做什么才能实现这一目标?
编辑
在故事板中
![在此处输入图像描述][1]
在模拟器中
![在此处输入图像描述][2]
谢谢你们的帮助。 非常感激。
代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[scrollView setScrollEnabled:YES];
[self.view addSubview:cockTailNameLabel];
[self.view insertSubview:txtIngredients belowSubview:cockTailNameLabel];
[self.view insertSubview:scrollView belowSubview:cockTailNameLabel];
//[scrollView]
[self.cockTailNameLabel setText:self.passcockTailName];
[_txtUse setText:self.passUse];
[_txtUse setEditable:NO];
[_txtUse setUserInteractionEnabled:NO];
CGRect useFrame = _txtUse.frame;
useFrame.size.height = _txtUse.contentSize.height;
_txtUse.frame = useFrame;
[txtIngredients setText:self.passIngredients];
[txtIngredients setEditable:NO];
[txtIngredients setUserInteractionEnabled:NO];
CGRect ingredientFrame = txtIngredients.frame;
ingredientFrame.size.height = txtIngredients.contentSize.height;
txtIngredients.frame = ingredientFrame;
[txtRecipe setText:self.passReceipe];
[txtRecipe setEditable:NO];
[txtRecipe setUserInteractionEnabled:NO];
CGRect recipeFrame = txtIngredients.frame;
recipeFrame.size.height = txtRecipe.contentSize.height;
txtRecipe.frame = recipeFrame;
[scrollView insertSubview:_txtUse belowSubview:cockTailNameLabel];
[scrollView insertSubview:titleIngredients belowSubview:_txtUse];
[scrollView insertSubview:txtIngredients belowSubview:titleIngredients];
[scrollView insertSubview:btnReceipe belowSubview:txtIngredients];
[scrollView insertSubview:btnHome belowSubview:txtIngredients];
[scrollView insertSubview:txtRecipe belowSubview:btnHome];
[scrollView insertSubview:btnfacebookShare belowSubview:txtRecipe];
[scrollView insertSubview:btnTwitterShare belowSubview:txtRecipe];
/*[scrollView addSubview:_txtUse];
[scrollView addSubview:titleIngredients];
[scrollView addSubview:txtIngredients];
[scrollView addSubview:btnReceipe];
[scrollView addSubview:btnHome];
[scrollView addSubview:txtRecipe];
[scrollView addSubview:btnfacebookShare];
[scrollView addSubview:btnTwitterShare];*/
[scrollView setContentSize:CGSizeMake(320, 1000)];
NSLog(@"RecipeName :%@ ",passcockTailName);
}
在 Storyboard 或 IB 中,您可以自由地重新排列它们。
在你做的代码中- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
。
在代码中(在 viewDidLoad 中):
UIScrollView *scroll =[[UIScrollView alloc] initWithFrame:CGrectMake(0,0 320, 480)];
[self.view addSubview:scroll];
// code to init your UI object
UILabel *uilabel1 = [[UIlabel alloc] initWithFrame:CGrectMake(10,10, 100, 40)]; // example
uilabel1.font = [UIFont systemFontOfSize:10];
uilabel1.text = @"UILabel1";
uilabel1.backgroundColor = [UIColor clearColor];
//
//
UILabel *uilabel2 = [[UIlabel alloc] initWithFrame:CGrectMake(10, 10 + 10 + uilabel1.frame.origin.y, 100, 40)]; // example
uilabel2.font = [UIFont systemFontOfSize:10];
uilabel2.text = @"UILabel2";
uilabel2.backgroundColor = [UIColor clearColor];
//
//
[scroll addSubview:uilabel1];
[uilabel1 releale];
[scroll addSubview:uilabel2];
[uilabel2 releale];
//
//
// in end
float offset = 10.0 * 2; // offset between uiobjects, N - number of objects
scroll.contentSize = CGSizeMake (0, 0, uilabel1.frame.size.height + uilabel2.frame.size.height + offset, 320);
请注意,您可以设置您的 uiobjects 的框架(和其他属性)并添加它以下降。
您可以告诉下一个视图从第一个视图的末尾开始,如下所示:
startYOfNextView = firstView.frame.origin.y position + firstView.frame.size.height;
对其他视图的其余部分执行相同操作。 如果您的视图具有可变的文本长度,您可能需要根据特定字体预先计算字符串的高度,例如:
CGSize maxSize = CGSizeMake(widthOfView, 9999999);
CGSize expectedSize = CGSizeMake(stringVar sizeWithFont:[UIFont fontWithName:@"Arial"] withMaxSize:maxSize lineBreakMode:UILineBreakModeWordWrap);
然后告诉您的动态视图使用 expectedSize 变量的高度,如下所示:
myDynamicView = [[UILabel alloc] initWithFrame:CGRectMake(..., expectedSize.height)];
您的问题是标签位于文本视图(重叠)之上,对吗?
在这种情况下,您正在动态修改 textview 的高度。 如果只是为了显示目的,您可以将文本设置为 numberOfLines = 0 而不是 UITextview 的 UILabel; 由于它添加到滚动视图,添加标签就可以了。
您还需要修改其余视图的 origin.y 以便正确对齐。 即 secondView.origin.y = firstView.origin.y + firstView.size.height + 偏移量。 同样修改其余视图的原点,并在上面的视图中进行修改。 这样它的原点将位于前一个视图框架之外。
在我的情况下,我不得不将它投影到父视图上,所以请记住还有一些东西,
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
我认为添加一个透明的 UI 按钮是最快的解决方案。 您可以添加一次,然后在需要禁用当前视图下方的所有视图时显示/隐藏(假设“当前”视图,例如弹出窗口,最后添加到您的超级视图中)。
声明一个实例变量UIButton* _parentDisablingOverlay;
在您的弹出视图中,然后将其添加到其init
或layoutSubviews
方法中:
_parentDisablingOverlay = [[UIButton alloc] initWithFrame:CGRectMake(0,0, self.superview.frame.size.width, self.superview.frame.size.height)];
[self.superview insertSubview:_parentDisablingOverlay belowSubview:self];
_parentDisablingOverlay.hidden = true;
稍后,当您想要显示弹出窗口并禁用以下所有内容时:
- (void) open {
self.hidden = false;
_parentDisablingOverlay.hidden = false;
}
类似于关闭并重新启用所有内容:
- (void) close {
self.hidden = true;
_parentDisablingOverlay.hidden = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.