How to make an UIPickerView with a Done button?
Add this code to your viewDidLoad()
instead of method
let picker: UIPickerView
picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
picker.backgroundColor = .whiteColor()
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self. donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self. donePicker))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
textField1.inputView = picker
textField1.inputAccessoryView = toolBar
display done button on UIPickerview
You can use this code,
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,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;
and set button action method for changeDateFromLabel:
-(void)changeDateFromLabel:(id)sender
{
[[your UI Element] resignFirstResponder];
}
Show a UIPickerView when a button is tapped
If you want to do it from Interface Builder then you can add UIPickerView
in your storyBoard and initially hide that UIPickerView
and when your button tapped show it.
If you want to do this by coding then you can try this.
Define this as global.
var toolBar = UIToolbar()
var picker = UIPickerView()
Add below code inside your button tap action. I have added Done
button in toolbar to dismiss picker.
Note : I write entire code in button action if you don't want to do that just write all code in viewDidLoad
and write only these line in your button action self.view.addSubview(picker)
and self.view.addSubview(toolBar)
@IBAction func YOUR_BUTTON__TAP_ACTION(_ sender: UIButton) {
picker = UIPickerView.init()
picker.delegate = self
picker.dataSource = self
picker.backgroundColor = UIColor.white
picker.setValue(UIColor.black, forKey: "textColor")
picker.autoresizingMask = .flexibleWidth
picker.contentMode = .center
picker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
self.view.addSubview(picker)
toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
toolBar.barStyle = .blackTranslucent
toolBar.items = [UIBarButtonItem.init(title: "Done", style: .done, target: self, action: #selector(onDoneButtonTapped))]
self.view.addSubview(toolBar)
}
On Done button you can remove toolBar as well as PickerView.
@objc func onDoneButtonTapped() {
toolBar.removeFromSuperview()
picker.removeFromSuperview()
}
Delegate methods of UIPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return YOUR_DATA_ARRAY.COUNT
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return YOUR_DATA_ARRAY[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(YOUR_DATA_ARRAY[row])
}
Done Toolbar on UIPickerView
It seems like UIPickerView is not letting its children receive touch events. If you are showing this in conjunction with UITextField you can use it's inputView and inputAccessoryView to acheive the same. Alternatively you can create a top level container UIView to hold both toolbar and picker view like this:
let picker = UIView(frame: CGRect(x: 0, y: view.frame.height - 260, width: view.frame.width, height: 260))
// Toolbar
let btnDone = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.monthdoneButtonAction))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelClick))
let barAccessory = UIToolbar(frame: CGRect(x: 0, y: 0, width: picker.frame.width, height: 44))
barAccessory.barStyle = .default
barAccessory.isTranslucent = false
barAccessory.items = [cancelButton, spaceButton, btnDone]
picker.addSubview(barAccessory)
// Month UIPIckerView
monthPicker = UIPickerView(frame: CGRect(x: 0, y: barAccessory.frame.height, width: view.frame.width, height: picker.frame.height-barAccessory.frame.height))
monthPicker.delegate = self
monthPicker.dataSource = self
monthpickerData = ["January","February","March","April","May","June","July","August","September","October","November","December"]
monthPicker.backgroundColor = UIColor.white
picker.addSubview(monthPicker)
how to add done button on uipicker view
You can use this . first add UIView in your self.view. add UIPickerView
and UIToolBar
init. and bind IBAction method with UIBarButtonItem. Please check attach screenshot.
Maybe this will help you.
How do I confirm my selection in UIPickerView with a done button?
@objc func donePressed(_ sender: UIButton) {
print("Done pressed")
print(strings[pickerView!.selectedRow(inComponent: 0)])
}
UIPickerview done button not working, even its not calling selector method
for example I add this
Step-1
Create the One UIPicker
,UItextField
and NSArray for load the details
@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{
UITextField *myTextField;
UIPickerView *myPickerView;
NSArray *pickerArray;
}
@end
Step-2
on your ViewDidLoad
call the picker method like
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addPickerView];
}
Step-3
create the picker, textfield and done button
-(void)addPickerView{
pickerArray = [[NSArray alloc]initWithObjects:@"Chess",
@"Cricket",@"Football",@"Tennis",@"Volleyball", nil];
myTextField = [[UITextField alloc]initWithFrame:
CGRectMake(10, 100, 300, 30)];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.delegate = self;
[self.view addSubview:myTextField];
[myTextField setPlaceholder:@"Pick a Sport"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
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-
myPickerView.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
myTextField.inputView = myPickerView;
myTextField.inputAccessoryView = toolBar;
}
Step-4
if user press the Done Button resign the picker
-(void)done:(id)sender
{
[myTextField resignFirstResponder];
}
Step-5
PickerView Delegate methods
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[myTextField setText:[pickerArray objectAtIndex:row]];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component{
return [pickerArray objectAtIndex:row];
}
here I attached the sample project
Sample Output like
Update
if you are used the button hidden the pickerview use this
-(void)donePressed:(id)sender
{
[self.myPickerView removeFromSuperview];
}
when you pressed the button call this method
[self funtion];
Related Topics
How to Get Available Wifi Network Name in iOS Using Swift
How to Insert 40000 Records Fast into an SQLite Database in an iPad
Nehotspothelper Annotations Not Appearing
Warning: Uicollectionviewflowlayout Has Cached Frame Mismatch for Index Path 'Abc'
The Advertisingidentifier and Identifierforvendor Return "00000000-0000-0000-0000-000000000000"
Adding Convenience Initializers in Swift Subclass
How to Display Clickable Links in Uitextview
iOS Localization: Unicode Character Escape Sequences, Which Have the Form '\Uxxxx' Does Not Work
iOS Wifi Scan, Signal Strength
How to Create Auto Incremented Key in Firebase
Is It Possible For Uistackview to Scroll
Convert String to Nsurl Is Return Nil in Swift
Why Is Uiscrollview Leaving Space on Top and Does Not Scroll to the Bottom
Remove Space Navigationtitle But Not the Back Button
Storing and Retrieving Image in SQLite with Swift
Difference Between Presentviewcontroller and Uinavigationcontroller