4

在我的应用程序中,我正在创建混合的视图控制器,UILabel并且UITextview它希望可以滚动,因为文本是动态的并且也超过了垂直屏幕大小。

我目前有 Scrollview,它的子视图如下。视图是在Xcode 4.3 Storyboard中创建的。

UILabel1(说标题)

UITextView1(可以是任意大小的动态文本)

UILabel2(第二个标题)

UITextView2(可以是任意大小的动态文本)

等等。

问题是

UITextView1内容更多时,它会与UILabel2我不想要的内容重叠。

我想在UILabel1scrollView 之上和. 下面 等等。UITextView1UILabel1UILabel2UITextView1

我必须做些什么来实现这一目标?

编辑

在故事板中

![在此处输入图像描述][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);

}

4

6 回答 6

10

在 Storyboard 或 IB 中,您可以自由地重新排列它们。

在你做的代码中- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview

于 2012-08-07T21:28:55.840 回答
1

在代码中(在 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 的框架(和其他属性)并添加它以便下降。

于 2012-08-07T21:39:23.460 回答
0

您可以告诉下一个视图从第一个视图的末尾开始,如下所示:

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)];
于 2012-08-08T02:59:56.500 回答
0

您的问题是标签出现在文本视图之上(重叠),对吗?

在这种情况下,您正在动态修改 textview 的高度。如果只是出于显示目的,您可以将文本设置为带有 numberOfLines = 0 的 UILabel 而不是 UITextview; 随着它添加到滚动视图中添加标签会很好。

您还需要修改剩余视图的 origin.y 以便正确对齐。即,secondView.origin.y = firstView.origin.y + firstView.size.height + offset。同样修改上面视图的剩余视图的原点。这样它的原点将位于前一个视图框架之外。

于 2012-08-08T18:26:07.783 回答
0

就我而言,我必须将它投影到父视图上,所以请记住还有一些东西,

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
于 2019-12-20T09:49:59.677 回答
0

我认为添加一个透明的 UI 按钮是最快的解决方案。您可以添加一次,然后在需要禁用当前视图下方的所有视图时显示/隐藏(假设“当前”视图,例如弹出窗口,最后添加到您的超级视图)。

在弹出视图中声明一个实例变量UIButton* _parentDisablingOverlay;,然后在它的initorlayoutSubviews方法中添加它:

_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;
}
于 2020-07-24T21:45:15.620 回答