我在viewDidLoad方法中编写了以下代码:
categoryPickerView=[[UIPickerView alloc]init]; categoryPickerView.alpha = 0; [self.view addSubview:categoryPickerView]; categoryPickerView.delegate=self; categoryPickerView.tag=1;
并称此方法隐藏选择器视图
- (IBAction)hidePickerView:(id)sender { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.6]; CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0,200); categoryPickerView.transform = transfrom; categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1; [UIView commitAnimations]; }
我的问题是我想在选择器视图上显示一个“完成”按钮,并且选择器视图应该隐藏在按钮上.
解决方法
你可以使用这段代码,
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,320,44)]; [toolBar setBarStyle:UIBarStyleBlackOpaque]; UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(changeDateFromLabel:)]; toolBar.items = @[barButtonDone]; barButtonDone.tintColor=[UIColor blackColor]; [pickerView addSubview:toolBar]; //(or)pickerView.inputAccessoryView = toolBar;
并为changeDateFromLabel设置按钮操作方法:
-(void)changeDateFromLabel:(id)sender { [[your UI Element] resignFirstResponder]; }