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

时间:2014-07-04 07:24:53

标签: ios uiview uiscrollview

我正在尝试创建一个应用程序,我在界面构建器中添加了一个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];

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

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

这是正确的方式还是有其他方式。

希望问题清楚

提前致谢

1 个答案:

答案 0 :(得分:3)

这是代码,它会起作用,但我不确定这是否是您要搜索的内容

- (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到superview的前导,顶部,尾随,底部约束,都等于0.并且您使用键盘外观的通知。尝试一下,告诉我你是否还有问题。