以编程方式添加 UIPickerView
Adding UIPickerView Programmatically
我正在尝试以编程方式添加 UIPickerView
,但我没有得到想要的结果。
我有一个滚动视图,我在其中添加了一些 UITextFields
,如下图所示。
这个想法是,当用户选择 DATE
时,会显示 UIDatePicker
并且用户选择 DATE
。按下 DATE
选择器 UIBarButtonItem
上的 NEXT
按钮(我也以编程方式添加),TIME
文本字段获得焦点,UIDatePicker
(默认为时间) 显示。对于 DATE
和 TIME
字段,我使用了 UIDatePicker
并且这是 100%。
对于剩余的 3 个文本字段,我尝试做同样的事情,但使用 UIPickerView
代替,因为它们不是 DATE
或 TIME
.[=40 类型=]
但是,当我 运行 我的代码时,我得到以下结果,如下所示。可能很难从图像中辨认出来,但是 behind/over 底部的黄色 SEARCH
按钮应该是 UIPickerView
(可以看到 PROFILE
和 SEARCH
条项目)
我的 UIPickerViewDelegate
方法(numberOfComponentsInPickerView
、numberOfRowsInComponent
、titleForRow
和 didSelectRow
)运行 当视图首次出现但不再出现时从我执行程序时所看到的。在这里,我定义了在国家或县文本字段中显示的数据数组。
下面是到目前为止我必须以编程方式显示 UIPickerViews
的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
[self initPickers];
}
- (void) initPickers
{
// Used for all other reusable UIPickerViews
pickerView = [[UIPickerView alloc] init];
}
然后当例如COUNTRY 文本字段获得焦点(这是经过测试和工作的),我按如下方式为 UIPickerView 设置动画:
- (BOOL) textFieldShouldBeginEditing:(UITextField*) textField
{
activeTextField = textField;
if (activeTextField.tag == 2)
{
self.countryTextField.inputView = pickerView;
// We are on the Country Text Field
currentArray = countryDataArray;
NSLog(@"Array Count %ld", [currentArray count]); // Is populated with data
[self animatePickerViewIn];
return NO;
}
return YES;
}
-(void) animatePickerViewIn
{
// Get the screen width and size
float screenWidth = [[UIScreen mainScreen] bounds].size.width;
float screenHeight = [[UIScreen mainScreen] bounds].size.height;
// Calculate the starting X Coordinate
float pickerWidth = screenWidth;
float pickerHeight = pickerView.frame.size.height;
float pickerViewStartinYCoordinate = screenHeight - pickerView.frame.size.height;
// Animate in and set PickerView to dimensions and coordinates
[UIView animateWithDuration:0.25 animations:^{
[pickerView setFrame:CGRectMake(0, pickerViewStartinYCoordinate, pickerWidth, pickerHeight)];
[self.view addSubview:pickerView];
}];
}
任何人都可以解释为什么我没有看到完整的 UIPickerView 吗?
正确的做法是将日期选择器设置为文本字段的 inputView
。这样,当文本字段成为第一响应者时,UIKit 将负责将日期选择器动画化到屏幕上(如果适用)。您还可以使用下一步按钮将工具栏设置为 inputAccessoryView
,UIKit 也会负责显示它。
如果您只是将日期选择器和选择器视图设置为文本字段的输入视图,则如下所示:
这是我的演示应用程序的源代码:https://github.com/mayoff/ios-picker-input-views
我正在尝试以编程方式添加 UIPickerView
,但我没有得到想要的结果。
我有一个滚动视图,我在其中添加了一些 UITextFields
,如下图所示。
这个想法是,当用户选择 DATE
时,会显示 UIDatePicker
并且用户选择 DATE
。按下 DATE
选择器 UIBarButtonItem
上的 NEXT
按钮(我也以编程方式添加),TIME
文本字段获得焦点,UIDatePicker
(默认为时间) 显示。对于 DATE
和 TIME
字段,我使用了 UIDatePicker
并且这是 100%。
对于剩余的 3 个文本字段,我尝试做同样的事情,但使用 UIPickerView
代替,因为它们不是 DATE
或 TIME
.[=40 类型=]
但是,当我 运行 我的代码时,我得到以下结果,如下所示。可能很难从图像中辨认出来,但是 behind/over 底部的黄色 SEARCH
按钮应该是 UIPickerView
(可以看到 PROFILE
和 SEARCH
条项目)
我的 UIPickerViewDelegate
方法(numberOfComponentsInPickerView
、numberOfRowsInComponent
、titleForRow
和 didSelectRow
)运行 当视图首次出现但不再出现时从我执行程序时所看到的。在这里,我定义了在国家或县文本字段中显示的数据数组。
下面是到目前为止我必须以编程方式显示 UIPickerViews
的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
[self initPickers];
}
- (void) initPickers
{
// Used for all other reusable UIPickerViews
pickerView = [[UIPickerView alloc] init];
}
然后当例如COUNTRY 文本字段获得焦点(这是经过测试和工作的),我按如下方式为 UIPickerView 设置动画:
- (BOOL) textFieldShouldBeginEditing:(UITextField*) textField
{
activeTextField = textField;
if (activeTextField.tag == 2)
{
self.countryTextField.inputView = pickerView;
// We are on the Country Text Field
currentArray = countryDataArray;
NSLog(@"Array Count %ld", [currentArray count]); // Is populated with data
[self animatePickerViewIn];
return NO;
}
return YES;
}
-(void) animatePickerViewIn
{
// Get the screen width and size
float screenWidth = [[UIScreen mainScreen] bounds].size.width;
float screenHeight = [[UIScreen mainScreen] bounds].size.height;
// Calculate the starting X Coordinate
float pickerWidth = screenWidth;
float pickerHeight = pickerView.frame.size.height;
float pickerViewStartinYCoordinate = screenHeight - pickerView.frame.size.height;
// Animate in and set PickerView to dimensions and coordinates
[UIView animateWithDuration:0.25 animations:^{
[pickerView setFrame:CGRectMake(0, pickerViewStartinYCoordinate, pickerWidth, pickerHeight)];
[self.view addSubview:pickerView];
}];
}
任何人都可以解释为什么我没有看到完整的 UIPickerView 吗?
正确的做法是将日期选择器设置为文本字段的 inputView
。这样,当文本字段成为第一响应者时,UIKit 将负责将日期选择器动画化到屏幕上(如果适用)。您还可以使用下一步按钮将工具栏设置为 inputAccessoryView
,UIKit 也会负责显示它。
如果您只是将日期选择器和选择器视图设置为文本字段的输入视图,则如下所示:
这是我的演示应用程序的源代码:https://github.com/mayoff/ios-picker-input-views