ios - 如何在水平分页 ScrollView 中添加日期选择器?

标签 ios objective-c iphone cocoa ios5

我已经创建了带分页的 UIScrollview,但我想在某些页面中添加日期选择器。但日期选择器滚动不起作用。

NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [colors objectAtIndex:i];
    CGRect pickerFrame = CGRectMake(0,100,100,100);

    UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:myPicker];
    [myPicker release];
    [self.scrollView addSubview:subview];
    [subview release];
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);

self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;

最佳答案

不是将选择器 View 添加到 self.view,而是将其添加到 self.scrollView

[self.scrollView addSubview:myPicker];

否则,您可以将选择器 View 作为输入 View 添加到文本字段,

for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [colors objectAtIndex:i];
    //CGRect pickerFrame = CGRectMake(0,100,100,100);

    myTextField = [[UITextField alloc]initWithFrame:
                                CGRectMake(10, 100, 300, 30)]; //if you dont want to show the text field you can change the frame to CGRectMake(0, 0, 1, 1)
    myTextField.borderStyle = UITextBorderStyleRoundedRect;
    myTextField.textAlignment = NSTextAlignmentCenter;
    myTextField.delegate = self;
    [self.view addSubview:myTextField];

    UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    [myTextField setInputView:myPicker];
    [self.scrollView addSubview:myTextField];


    [myPicker release];

    [self.scrollView addSubview:subview];
    [subview release];
}

当你想显示选择器时,调用下面的方法[self showPickerView]

- (void)showPickerView
{
    [myTextField becomeFirstResponder];
}

关于ios - 如何在水平分页 ScrollView 中添加日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28103667/

相关文章:

ios - AFNetworking 请求失败 : unacceptable content-type: text/html

iphone - 使用包装器时,语句不会在 sqlite3 中提交

iphone - 继承 UINavigationController 类和分配 rootViewController 的区别

ios - 在 Swift 中的按钮上设置图像/图标

ios - 找不到 Pod CoreData+MagicalRecord.h 文件

ios - 在 iDevice 锁定状态下访问文档目录的任何其他方法?

ios - layoutIfNeeded 是如何使用的?

ios - 使用本地化的 app.staticTexts 进行 UI 测试 (XCTest)

ios - Swift:TextFields 总是显示布局约束错误

ios - iPhone 应用程序 - 基于经度和纬度的距​​离