为Picker

时间:2017-01-02 22:44:23

标签: ios objective-c uitoolbar

我正在尝试在选择器顶部添加完成按钮,如下所示。但不幸的是,我无法看到完成按钮。

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                                   target:self action:@selector(done)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                          CGRectMake(0, self.view.frame.size.height-
                                     picker.frame.size.height-250, self.view.frame.size.width, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
                             doneButton, nil];
[toolBar setItems:toolbarItems];
categoryTF.inputView = picker;

3 个答案:

答案 0 :(得分:0)

您忘记将工具栏添加到视图中。你可以这样做:

[self.view addSubview:toolBar];

答案 1 :(得分:0)

添加此行:
    

categoryTF.inputAccessoryView = toolBar;

答案 2 :(得分:0)

使用工具栏和完成按钮进行pickerView

CGRect pickerFrame = CGRectMake(0,kSCREEN_HEIGHT-200,kSCREEN_WIDTH,200);
UIPickerView * pickerview = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerview.delegate = self; 
pickerview.dataSource = self;      
textField.inputView=pickerview;

UIToolbar *myToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 56)];
myToolbar.barStyle=UIBarStyleBlack;
[myToolbar sizeToFit];
myToolbar.backgroundColor=[UIColor whiteColor];
NSMutableArray *barItems=[[NSMutableArray alloc]init];
UIBarButtonItem *btnItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:btnItem];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myToolbar setItems:barItems animated:YES];
myToolbar.barStyle = UIBarButtonItemStylePlain;
myToolbar.barTintColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.96f alpha:1.0f];
myToolbar.tintColor=[UIColor blackColor];
txtField.inputAccessoryView=myToolbar;

-(void)pickerDoneClicked
{  
[txtField resignFirstResponder];
}