在uiview中添加动态标签然后在uiview中添加滚动视图



我试图使一个应用程序中,我在界面构建器中添加一个scrollview,然后在。m文件中,我在一个UIView中动态添加两个标签,然后这个UIView在scrollview中动态添加,这是在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];

生成标签,但问题是我无法向下滚动它们。

我不知道是什么问题。我是新手。

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

希望问题是清楚的

Thanks in advance

这是代码,它将工作,但我不确定这是否是你正在搜索的

- (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);

}

这是整个代码。对我来说是可行的,但在故事板中,你必须给scrollView添加约束。从scrollView到父视图的前导,顶,尾,底约束,都等于0。你用通知来显示键盘的外观。

最新更新