Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Programmatically add picker to tableview with same behavior as default keyboard
I have a tableview with a few cells. One of these contains a textfield, which shows the keyboard when selected.
Now in the tableviewcontroller class I create a UIPickerView in the viewWillAppear method, like this:
moodPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; moodPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; CGSize pickerSize = [moodPickerView sizeThatFits:CGSizeZero]; moodPickerView.frame = [self pickerFrameWithSize:pickerSize]; moodPickerView.showsSelectionIndicator = YES; // note this is default to NO moodPickerView.dataSource = self; moodPickerView.delegate = self; moodPickerView.hidden = YES; [self.view addSubview:moodPickerView];
When a certain cell is selected I want to show the picker, like so:
[picker setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height+(picker.frame.size.height/2))]; // place the pickerView outside the screen boundaries [picker setHidden:NO]; // set it to visible and then animate it to slide up [UIView beginAnimations:@\"slideIn\" context:nil]; [picker setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-(picker.frame.size.height/2))]; [UIView commitAnimations];
While the keyboard is up, I can still scroll in the tableview while the keyboard stays at the bottom of the screen. But, when the pickerview is up I can't.
How do I show the pickerview so it has the same behavior as the keyboard?
First off, you're doing it wrong if you're adding subviews in viewWillAppear. You should be using viewDidLoad to create your picker instance and add it to the subview.
Can you NSLog the frame of your picker when you are showing it? I suspect the picker's frame is likely the entire screen, which means it would be blocking touches from going to the tableview.
First off, you're doing it wrong if you're adding subviews in viewWillAppear. You should be using viewDidLoad to create your picker instance and add it to the subview.
Thanks for this, I was wondering about this myself.
smithdale87;427429 said:
Can you NSLog the frame of your picker when you are showing it? I suspect the picker's frame is likely the entire screen, which means it would be blocking touches from going to the tableview.
NSLog(@\"picker view frame: %f x %f\", picker.frame.size.width, picker.frame.size.height);
returns: picker view frame: 320.000000 x 216.000000
The touches aren't being blocked as I can select the row above it.
UITextFields have an inputView property. This replaces the keyboard. Set this property in cellForRow.
I'm a bit confused by this. The cell with the pickerview doesn't have a textfield, just a label that get's updated when the picker value changes. Also, I currently don't use cellForRow. The tableview uses static cells, using storyboard/ib.
One of these contains a textlabel, which shows the keyboard when selected.
You say that you have a text label. I assume it's a UITextField since it shows a keyboard when selected. UITextFields have an inputView property which you can set to a view. In your case, you should set this property to your UIPickerView. You should add yourself as a target for UIControlEventValueChanged events from the UIPickerView. In the method that gets called when the value changes, update the UITextField.
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
You say that you have a text label. I assume it's a UITextField since it shows a keyboard when selected.
Yes you're right, I meant textfield (updated my post). But the textfield is in a different cell, one that requires the keyboard. The other cell however has a UILabel and shows a pickerview when selected (via didSelectRowAtIndexPath).
So why not just replace it with a UITextField? It's the same thing except it summons a keyboard automatically and you can replace that keyboard with your own view.
And to get the same functionality of clicking anywhere in the cell to bring up the UIPickerView, just call becomeFirstResponder on the UITextField in didSelectRow.
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
So why not just replace it with a UITextField? It's the same thing except it summons a keyboard automatically and you can replace that keyboard with your own view.
And to get the same functionality of clicking anywhere in the cell to bring up the UIPickerView, just call becomeFirstResponder on the UITextField in didSelectRow.
Yeah, this seems to be the way to go. Can I hide the blinking cursor in the textfields? It's weird to see the cursor when selecting from a picker.
Replies
Can you NSLog the frame of your picker when you are showing it? I suspect the picker's frame is likely the entire screen, which means it would be blocking touches from going to the tableview.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNew app - See screenshots and details at www.globaclock.com.
If you want to
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomereturns: picker view frame: 320.000000 x 216.000000
The touches aren't being blocked as I can select the row above it.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNew app - See screenshots and details at www.globaclock.com.
If you want to
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAnd to get the same functionality of clicking anywhere in the cell to bring up the UIPickerView, just call becomeFirstResponder on the UITextField in didSelectRow.
New app - See screenshots and details at www.globaclock.com.
If you want to
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNew app - See screenshots and details at www.globaclock.com.
If you want to
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThank you for your help!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome