I am trying to implement UIPickerView and I have almost done it. But when I click on text field, picker view appears behind the text field. Here are the snapshots before and after showing picker view.
Before clicking on text field.
After clicking on the text field.
Here you can see that picker view is behind the text field. I want it to be on top.
EDIT Here is the code I am using to show picker view:
-(void) showPickerView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.view bringSubviewToFront:self.picker];
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if(iOSDeviceScreenSize.height == 480)
{
CGRect frame = self.picker.frame;
frame.origin.y = 151.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 107.0;
self.toolBar.frame = frame;
}
else if(iOSDeviceScreenSize.height == 568)
{
CGRect frame = self.picker.frame;
frame.origin.y = 239.0;
self.picker.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = 195.0;
self.toolBar.frame = frame;
}
[UIView commitAnimations];
}
In your view hierarchy the textfield may be above pickerview. If you are using xib/storyboard then select pickerview and from menu select Editor>Arrange>Sent to Front.
If you are using code for creating all ui elements add pickerview after adding textfield. or
try
[self.view bringSubViewToFront:pickerView];
use
[self.view bringSubViewToFront:pickerView]
you did't get than please share your code for more explanation we will help..
I solved the issue. In fact it was a plum mistake. I placed the pickerView before other views. So when it appears on screen, it appears behind the textfield. I had to put it at the bottom place in my .nib file.
In iOS, the view added before will go behind other views. I missed this concept.
Related
I've been following this and a bunch of other tutorials/blogs,etc trying to get my view to scroll when the user hits "return" and ends on a UITextField that is being covered by the keyboard but nothing is actually working.
I'm wondering what must I be doing or missing that is causing this?
Basically:
the user wants to do something in the app: add a credit card
show a UIView that contains all the CC fields
1/2 of the UITextFields are covered by the keyboard
scroll the view when the user gets there.
Nothing is happening. The code for detecting the 'covered by keyboard' case is being hit and executed but: [self.scrollView setContentOffset:scrollPoint animated:YES] has no effect at all.
Thoughts?
Reveal screenshot:
code: same as the tutorial in the link. Nothing new there.
Did you check the contentSize property?
To make a scroll view scrollable, the content must be larger than the display area(usually the bounds of the scrollview).
You can achieve this by explicitly setting the contentSize property which is CGSizeZero by default
In addition, setting the contentInset property to adjust the size of display area(in this case you can set bottom value equals keyboard height) when the keyboard is popped up
Try this,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y-keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
and
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.25 animations:^{
[yourTextField setFrame:CGRectMake(yourTextField.frame.origin.x, yourTextField.frame.origin.y+keyboardHeight, yourTextField.frame.size.width, yourTextField.frame.size.height)];
}];
}
What I do to avoid the textField being covered by the keyboard is to animate the textfield to the top of the screen when the user touches it to start entering something with the keyboard and animate it back when done. It is much more elegant in my opinion. Maybe that could be an alternative solution for you... I do it for the different screensizes with different numbers... you might need to adjust it of course...
So listen to the textField delegate and in
- (void)textFieldDidBeginEditing:(UITextField *)textField {
you do
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 568){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -196; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 667){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -238; [self.view setFrame:frame]; [UIView commitAnimations];});
}
if(result.height == 736){
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = -251; [self.view setFrame:frame]; [UIView commitAnimations];});
}
and in
- (void)textFieldDidEndEditing:(UITextField *)textField {
you just animate back
({[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.35f]; CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations];});
My storyboard elements are subviews of containerView and containerView is a subview of the main view. I am trying to resize the height of my container view when an ad is available to show but I cannot get that to work. I am able to offset the view up, but I am not able to resize it. I have seen quite a bit a posts about this, but all suggestions I've read basically say to confirm Autolayout is not checked. I have confirmed that Autolayout is not checked in each element of my storyboard but still am not having success with this. The ad banner (placed just off screen at [0, 480] pops up nicely from the bottom just like I want it to, but it covers up my storyboard elements which is just plain UNACCEPTABLE. I will not stand for this! I need some help guys and gals…Please see code below:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
_containerView.frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
Try it like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
CGRect frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
[_containerView setFrame:frame];
CGRect frame2 = banner.frame;
frame2.origin.x = 0;
frame2.origin.y = -50;
[banner setFrame:frame2];
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
You have just changed the frame of your containerView. This does not in any way change the frames of the subviews of containerView. You either have to make containerView a scrollview or change the frames of every component.
In my iPhone app, I have two views on a main view controller , one is normal view 'a' with some buttons and other is scrollView 'b'. When I tap on button in view 'a', I want to show scroll view with animations like the scroll view has to come up from the back of view 'a', but it comes up from the front of view 'a'.
I used the following code for animating the scroll view.
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
}];
How to implement the animation to scroll view to show it from top of view 'a'.
Try to move "b" to the back first:
[self.view sendSubviewToBack:scrollview_b];
I found solution for my problem.
Solution :
CGRect Frame = scrollView.frame;
if(Frame.origin.y == 420){
Frame.origin.y = 298;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}else{
Frame.origin.y = 420;
[UIView animateWithDuration:0.5 animations:^{
scrollView.frame = Frame;
[self.view insertSubview:scrollView belowSubview:view_a];
}];
}
I want to create a menu that appears sliding from a side of my view, so I programmatically created a UIView and a UITableView, but, when it appears I can't select the cells in my view, here's the code where I created the view and the table:
paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 400)];
[paintView setBackgroundColor:[UIColor clearColor]];
tablaConfig = [[UITableView alloc]initWithFrame:CGRectMake(-160, 0, 160, 400) style:UITableViewStylePlain];
tablaConfig.delegate = self;
tablaConfig.dataSource = self;
tablaConfig.userInteractionEnabled = YES;
tablaConfig.scrollEnabled = NO;
tablaConfig.backgroundColor = [UIColor lightGrayColor];
[paintView addSubview:tablaConfig];
And here's the code where I make the table appear and disappear:
- (IBAction)btnTablaConfig:(id)sender {
if (selector) {
if (self.view.frame.origin.x == 0) {
//[self.view addSubview:paintView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect rect = self.view.frame;
// Move right.
rect.origin.x += 160;
rect.size.width -= 160;
self.view.frame = rect;
[UIView commitAnimations];
selector = false;
tablaConfig.userInteractionEnabled = YES;
}
}
else {
if (self.view.frame.origin.x == 160) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
CGRect rect = self.view.frame;
rect.origin.x -= 160;
rect.size.width += 160;
self.view.frame = rect;
[UIView commitAnimations];
selector = true;
tablaConfig.userInteractionEnabled = NO;
}
//[paintView removeFromSuperview];
}
}
Hope you can help
EDIT:
When I use this, I can select the cells, but lose the sliding effect, how can I get both?
- (IBAction)btnTablaConfig:(id)sender {
if (selector) {
[self.view addSubview:tablaConfig];
selector = false;
}
else {
[tablaConfig removeFromSuperview];
selector = true;
}
I also got this problem and I also didn't found that.
I am using NIDropDown custom control tableview shows but i cant select or scroll.
problem was that when I do [anySuperView addSubview:anyTableView];
My anySuperView's size (75) was less than my anyTableView's size (400).
so I made superViews size (500) and my problem solved.
Any body who can give more technical detail about this will be appreciated.
So late answering this because I found no solution anywhere else
I'm not quite sure what you're looking for. But if you want the cell to be highlighted when selected, you need to add this line of code to your tableView: cellForRowAtIndexPath:
yourcell.selectionstyle = UITableViewSelectionStyleBlue
You can also use UITableViewSelectionStyleGray. If you want to perform some actions when they select the row, you can use the tableviewdelegate methods:
Managing Selections
– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:
UITableViews can be tricky but stuff like this can be easily found in Apples references at:
http://developer.apple.com/library/ios/navigation/
Just Search for the class or delegate or whatever you are using.
It had a similar problem. I solved it by adding my table view farther down down the line and setting up a flag to keep track if it was installed. In your code, move [paintView addSubview:tablaConfig]; into the btnTablaConfig: method, executing it the first time the table is expanded. The problem seems to involve view init code timings.
I have a tableview controller and one of the cell is "date" and when i click i wanna show a datepicker and update the values.
When i click on limit date i get
Now here is the code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( indexPath.section == 0 )
{
if ( indexPath.row == 3 )
{
UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];
// check if our date picker is already on screen
if (self.pickerView.superview == nil)
{
[self.view.window addSubview: self.pickerView];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
//
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
self.pickerView.frame = startRect;
// compute the end frame
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height - pickerSize.height,
pickerSize.width,
pickerSize.height);
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
self.pickerView.frame = pickerRect;
// shrink the table vertical size to make room for the date picker
CGRect newFrame = self.tableView.frame;
newFrame.size.height -= self.pickerView.frame.size.height;
self.tableView.frame = newFrame;
[UIView commitAnimations];
// add the "Done" button to the nav bar
self.navigationItem.rightBarButtonItem = self.doneButton;
}
}
}
}
- (IBAction)dateChanged:(id)sender
{
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]].detailTextLabel.text = [self.dateFormatter stringFromDate:[sender date]];
}
- (void)slideDownDidStop
{
[self.pickerView removeFromSuperview];
}
- (IBAction)doneAction:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect endFrame = self.pickerView.frame;
endFrame.origin.y = screenRect.origin.y + screenRect.size.height;
// start the slide down animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(slideDownDidStop)];
self.pickerView.frame = endFrame;
[UIView commitAnimations];
// grow the table back again in vertical size to make room for the date picker
CGRect newFrame = self.tableView.frame;
newFrame.size.height += self.pickerView.frame.size.height;
self.tableView.frame = newFrame;
// remove the "Done" button in the nav bar
self.navigationItem.rightBarButtonItem = nil;
// deselect the current table row
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
self.navigationItem.rightBarButtonItem = self.nextButton;
}
I have three questions:
First: what is that black bar on top of datepicker and how do i remove it?
Second: what can i do for closing the datepicker when the user clicks outside the datepicker?
Third: How can i move the tableview scroll so that the field "limit date" can be visible when datepicker opens?
Sorry for the big screenshots.
What about presenting your date picker modally in UIActionSheet?
In the example above that black bar (which is probably a sizing issue) is replaced with a convenient toolbar in which you can place buttons like "save" and "cancel", and I believe that if you use a UIActionSheet and click outside of it it's automatically dismissed (I'm not sure about this), but if' it's not you can use it's dismiss method to hide it.
About the table view, you can use [myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; to set the offset of the table scroll at a particular cell you want.