Hiding/ Showing UIPickerView
UIPickerView inherits from UIView, so you should be able to just toggle its 'hidden' property:
if (pickerView) pickerView.hidden = !pickerView.hidden;
How to dismiss UIPickerView in iOS7 when I click the screen
Why don't you use -
-(IBAction)hideThePicker {
[_myTF resignFirstResponder];
pickerView.hidden = YES;
}
you can hide the UIPickerView
just by using its 'hidden' property.
EDIT-
Check updated answer above, you have to resign the text field responder as well.
ios7 - UITableViewCell with UIPickerView and UIDatePicker built inline
I use another - maybe simpler - solution to solve this.
Image that we have two cells
- Date label cell
- Date picker cell
Most of the "magic" is within the table view delegate's tableView:heightForRowAtIndexPath:
method:
- (CGFloat)tableView:(UITableView:)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat heightForRow = tableView.rowHeight;
if ([self isDatePickerRowAtIndexPath:indexPath]) {
heightForRow = (self.isDatePickerShown) ? heightOfDatePicker : 0.0;
}
return heightForRow;
}
So you simply "hide" the date picker by returning a height of 0.0
.
In the tableView:didSelectRowAtIndexPath:
method you do the toggling:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self isDateLabelRowAtIndexPath:indexPath])
{
self.datePickerIsShown = ! self.isDatePickerShown;
[tableView beginUpdates];
[tableView endUpdates];
}
}
Calling the empty beginUpdates
endUpdates
block forces the table to call the tableView:heightForRowAtIndexPath:
again (animated) and nicely fades in or out the date picker cell.
When the date picker cell is the last one in a section you might also want to update the date label cell's separatorInset
to UIEdgeInsetsZero
when the date picker is hidden and to the default value when it's shown.
EDIT:
For completeness: datePickerIsShown
is a simple boolean:
@property(nonatomic, getter = isDatePickerShown) BOOL datePickerIsShown;
The methods isDateLabelRowAtIndexPath:
and isDatePickerRowAtIndexPath:
are just helper methods that compare a given indexPath
to the known index path of the appropriate cell:
- (BOOL)isDatePickerRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([self.datePickerIndexPath compare:indexPath] == NSOrderedSame);
}
EDIT 2:
There's one additional step missing: Make sure that you set the date picker cell's clipsToBounds
property to YES
, otherwise you'll get some view glitches.
Related Topics
Download Xcode 4.2 Documentation for Offline Install
Linker Error in Xcode 5 for Libzbar.A
How to Detect Tableview Cell Touched or Clicked in Swift
How to Properly Change My Status Bar Style in Swift 2/ iOS 9
How to Get the Custom Overlay for Uiimagepicker Camera to Be Full Screen in iOS 7
Using Global Variables in Objective-C
Cropping Center Square of Uiimage
How to Rearrange Views When Autorotating with Autolayout
How to Expand Uicollectionview Contentsize When Paging Enable
How to Add Textfield to Uialertcontroller in Swift
Nspredicate to Test for Null, and Blank Strings
Custom Url Scheme for New Facebook iOS App
Populating Tableview with Multiple Sections and Multiple Dictionary in an Array in Swift
Pass Variables to a New View Controller via a Subclass and Dozens of Map Pins
How to Custom Modal View Controller Presenting Animation