在uiview中动态添加标签,然后将uiview添加到scrollview ios

在uiview中动态添加标签,然后将uiview添加到scrollview ios,ios,uiview,uiscrollview,Ios,Uiview,Uiscrollview,我正在尝试制作一个应用程序,其中我在界面生成器中添加一个scrollview,然后在.m文件中,我在UIView中动态添加两个标签,然后在scrollview中动态添加此UIView。这是在for循环中完成的 我用于scrollview的代码: // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionar

我正在尝试制作一个应用程序,其中我在界面生成器中添加一个scrollview,然后在.m文件中,我在UIView中动态添加两个标签,然后在scrollview中动态添加此UIView。这是在for循环中完成的

我用于scrollview的代码:

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height + kbSize.height);
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height);
}
添加两个标签的代码为:

UILabel  * label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 + Y,label1.frame.size.width, 21)];
label1.text = @"label1";
#and all label1 propertiies

UILabel  * label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 21 + Y, label2.frame.size.width, 21)];
label2.text = @"label2";

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view1.frame.size.width, 42)];

CGRect frame;
frame = view1.frame;
[self.scrollView setContentSize:(CGSize){ self.scrollView.contentSize.width,frame.size.height + kbSize.height}];
[self.scrollView addSubview:view1];
                [view1 addSubview:label1];
                [view1 addSubview:label2];
标签已生成,但问题是我无法向下滚动它们

我不知道是什么问题。我是新来的

这条路对吗?还有别的路吗

希望问题清楚


提前感谢

这是代码,它可以工作,但我不确定这是否是您正在搜索的内容

- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat height;
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, 320, 30)];
tf.borderStyle = UITextBorderStyleRoundedRect;
[self.scrollView addSubview:tf];
for(NSInteger i=1; i<10;i++) {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureMethod:)];
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, (i*100), 320, 100)];
    v.backgroundColor = [self randomColor];

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 20)];
    label1.text = @"Label1";

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, 320, 20)];
    label2.text = @"Label2";
    [v addSubview:label1];
    [v addSubview:label2];

    v.tag = i;
    [v addGestureRecognizer:tapGesture];
    [self.scrollView addSubview:v];
    height+=100;
}
height+=100;
self.scrollView.scrollEnabled = YES;
self.scrollView.contentSize = CGSizeMake(320, height);
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

/// the gesture selector method down HERE

// Do any additional setup after loading the view, typically from a nib.
}
- (void)gestureMethod:(id)sender {
UIView *view = [(UITapGestureRecognizer *)sender view];
//treat the gesture inside the view based on the tag.
NSLog(@"The tap is in the view with the tag: %ld",(long)view.tag);
}

 -(UIColor *)randomColor
{
NSInteger aRedValue = arc4random()%255;
NSInteger aGreenValue = arc4random()%255;
NSInteger aBlueValue = arc4random()%255;

UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];
return randColor;
}

 - (void)keyboardWillShow:(NSNotification*)notification {

NSDictionary* userInfo = [notification userInfo];

//scroll scrollView to bottom
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height, 0);
 }

- (void)keyboardWillHide:(NSNotification*)notification {
 self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);


}
-(void)viewDidLoad
{
[超级视图下载];
浮子高度;
UITextField*tf=[[UITextField alloc]initWithFrame:CGRectMake(0,20,320,30)];
tf.borderStyle=UITextBorderStyleRoundedRect;
[self.scrollView addSubview:tf];

对于(NSInteger i=1;i很可能您的contentSize小于ScrollView框架,或者它们相等。您必须将contentSize设置得更大。此外,不建议设置contentSize,最好使用contentInset,在这里您可以找到有关contentInset的更多信息。您可以编辑上述代码吗?@GeorgeK您检查了
frame.si的结果值吗ze.height+kbSize.height
请准确解释您想要什么,我将尝试复制,或者尝试给您一些advice@BogdanSomlea我有一个for循环,其中生成了一个uiview,在uiview中生成了两个标签,这个uiview在一个scrollview中每个uiview必须有2个标签。代码工作正常,但当显示键盘时,一些文本位于键盘下方。如何解决该问题?无法解决它…而且我有一个疑问,我想向每个视图添加点击功能,使用上面的代码我能做到吗?是的,你能做到,文本字段放在哪里?t代码对tap非常有效,但我遇到了标签的定位问题(生成的uiview工作不正常,请尝试触摸所有标签),当我搜索时,我发现我应该关闭自动布局,所以尝试了它,但不起作用,我知道我在寻求帮助,请查看我的编辑,请告诉我它是否起作用,我还添加了一个随机颜色的方法,这样我可以看到视图是否正确绘制,您可以删除它,但我认为它将帮助您