动态地将新的UITextView添加到UIscrollView

时间:2014-05-12 13:56:21

标签: ios view uiscrollview uitextview

我没有在我的应用程序中使用滚动视图,并且对其属性几乎没有经验。我希望滚动视图包含一个小UITextView,当用户点按添加按钮时,它会在前一个下方创建一个新的UITextView并在那里输入新数据。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.backgroundColor = [UIColor clearColor];

        UIScrollView *tempScroll = [UIScrollView new];

        self.logScroll = tempScroll;
        self.logScroll.frame = CGRectMake(0, 120, 320, 390);
        self.logScroll.contentSize= CGSizeMake(320,1280);
        self.logScroll.layer.cornerRadius = 10.0f;
        [self.logScroll showsVerticalScrollIndicator];
        self.logScroll.backgroundColor = [UIColor whiteColor];
        [self addSubview:tempScroll];

        UIButton *addWorkoutButton = [UIButton buttonWithType:UIButtonTypeSystem];
        addWorkoutButton.frame = CGRectMake(100, 0, 100, 30);
        [addWorkoutButton setTitle:@"Add Workout" forState:UIControlStateNormal];
        [addWorkoutButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [addWorkoutButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [addWorkoutButton addTarget:self action:@selector(addNewTextViewToLogScroll:) forControlEvents:UIControlEventTouchUpInside];
        [tempScroll addSubview:addWorkoutButton];

        [tempScroll setNeedsDisplay];


    }

    return self;
}

- (void) addNewTextViewToLogScroll:(id)sender {
    UITextField *newField = [[UITextField alloc]initWithFrame:CGRectMake(10,20,300,60)];
    newField.backgroundColor = [UIColor colorWithWhite:0.940 alpha:1.0];

    [logScroll addSubview:newField];
    [logScroll setNeedsDisplay];
}

这是我到目前为止所拥有的。现在我的问题是将新UITextField添加到滚动视图上的上一个UITextView下方。如何将其添加到上一个

下面

1 个答案:

答案 0 :(得分:0)

1.创建textField

UITextField *newField = [[UITextField alloc]initWithFrame:CGRectMake(10,10,100,20)];

2。将其添加到滚动视图

[yourScrollView addSubview:newField];

3.刷新滚动视图的内容以显示新添加的textField

[yourScrollView setNeedsDisplay];