水平UIScrollView,显示多个文本字段

时间:2015-10-31 10:01:32

标签: objective-c uiscrollview xcode6 uitextfield

我是Xcode的新手。我想制作一个包含多个页面的UIScrollView,每个页面都有多个UITextFields,每个页面都有所不同。我启用了分页UIScrollView。现在,我一直坚持在滚动页面上显示文本字段。

到目前为止,这是我的代码:

//set the paging to yes
self.scrollview.pagingEnabled = YES;
// create 5 pages
 NSUInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++)
{

    //set the origin of the sub view
    CGFloat myOrigin = i * self.view.frame.size.width;

    //create the sub view and allocate memory
    myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //create a label and add to the sub view
    CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
    textLabel = [[UILabel alloc] initWithFrame:myFrame];
    textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    textLabel.textAlignment =  NSTextAlignmentLeft;
    [myView addSubview:textLabel];

    //create a text field and add to the sub view
    myFrame.origin.y += myFrame.size.height + 10.0f;
    textField = [[UITextField alloc] initWithFrame:myFrame];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.tag = i+1;
    [myView addSubview:textField];
    //set the background to different color

    //set the scroll view delegate to self so that we can listen for changes
    self.scrollview.delegate = self;
    //add the subview to the scroll view
    [self.scrollview addSubview:myView];
}

//scroll horizontally
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
                                         self.scrollview.frame.size.height);

//we set the origin to the 1rd page
CGPoint scrollPoint = CGPointMake(self.view.frame.size.width * 0, 0);
//change the scroll view offset the the 1rd page so it will start from there
[scrollview setContentOffset:scrollPoint animated:YES];

[self.view addSubview:self.scrollview];

}

1 个答案:

答案 0 :(得分:0)

使用此代码textfieldCount是

for (int j=0; j < textFieldCount; j++)
    {
        myFrame.origin.y += myFrame.size.height + 10.0f*(j+1);
        UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.tag = j+1;
        [myView addSubview:textField];

    }

希望你得到答案。