2017-01-02 54 views
1

我想按如下方式在选取器顶部添加一个完成按钮。但不幸的是,我无法看到完成按钮。为Picker添加工具栏完成按钮

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; 

回答

0

您忘记了将您的工具栏添加到视图中。你可以做如下:

[self.view addSubview:toolBar]; 
0

加入这一行:

categoryTF.inputAccessoryView = toolBar;

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]; 
} 
相关问题