0

I made a textfield who's first responder is a datePicker. I also attached a toolBar to to a view, so when the textfield is tapped, the toolbar slides up with an animation. Then on the toolBar their is a done button that resigns the first responder. I also want it to remove the toolBar. To do this I added this

[pickerBar removeFromSuperview];

Then to reopen it I did this

[self.view addSubview:pickerBar];

So it is activated when the textview is touched.

The problem is when I tap the textfield again the toolBar loses its navigation.

Heres most of the code

pickerBar = [[UIToolbar alloc] init];
        pickerBar.barStyle=UIBarStyleBlackOpaque;
        [pickerBar sizeToFit];

        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
            CGRect pickerBarRect = CGRectMake(0, 568, 320, 44);
            [pickerBar setFrame:pickerBarRect];
        } else {
            CGRect pickerBarRect = CGRectMake(0, 480, 320, 44);
            [pickerBar setFrame:pickerBarRect];
        }

        //Set up the new position of the frame of the view

        NSMutableArray *barItems = [[NSMutableArray alloc] init];

        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(releasePicker)];
        [barItems addObject:doneBtn];

        [barItems addObject:doneBtn];
        [pickerBar setItems:barItems animated:YES];


        }
    }
}
- (void)releasePicker {
    [textfield resignFirstResponder];

    [pickerBar removeFromSuperview];
}

- (void)pickerActivated {

    [UIView animateWithDuration:.4 animations:^(void) {

    [self.view addSubview:pickerBar];



        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){

            CGRect rect = CGRectMake(0, 266, 320, 44);

            [pickerBar setFrame:rect];

            pickerBar.hidden = NO;

        } else {


            CGRect rect = CGRectMake(0, 178, 320, 44);

            [pickerBar setFrame:rect];




        }
}];

}
Adam Richardson
  • 2,518
  • 1
  • 27
  • 31
i_duhh_bomb
  • 300
  • 1
  • 10

2 Answers2

1

There is a difference between inputView and inputAccessoryView. This is what you want:

// setting the inputView
UIPickerView *pickerView = [[UIPickerView alloc] init];
// other configurations //
self.textField.inputView = pickerView;

// setting up the inputAccessoryView
UIToolbar *pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle = UIBarStyleBlackOpaque;
[pickerBar sizeToFit];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                           target:self 
                                                                           action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                         target:self 
                                                                         action:@selector(releasePicker)];
[pickerBar setItems:@[flexSpace, doneBtn] animated:YES];    
self.textField.inputAccessoryView = pickerBar;

This way the tool bar will be "attached" to your inputView and will automatically come up when the the text field is activated and disappear when your picker does!

Edit/Update

I did find this link that may help you, if you are interested in using a IB focused approach: https://stackoverflow.com/a/10705161/1429262

Community
  • 1
  • 1
Firo
  • 15,448
  • 3
  • 54
  • 74
  • I have tried this, but what happend was I had to move the toolbar to see my whole datePicker. Then it became dormant. – i_duhh_bomb Aug 07 '13 at 18:07
  • It is covering up part of your datePicker? You need to provide more infomration... – Firo Aug 07 '13 at 18:17
  • Idk what was different with yours than mine, but it looks great now. Although the only way to make it work was by deleting this line of code. `// setting the inputView UIPickerView *pickerView = [[UIPickerView alloc] init]; // other configurations // self.textField.inputView = pickerView;` – i_duhh_bomb Aug 08 '13 at 02:26
  • @user2660874 glad it worked, I just added those lines as a note to you that you need to setup the picker. I assumed you would use some other mechanism to do so. But glad it all worked out! – Firo Aug 08 '13 at 13:41
0

Far simpler is to use the inputAccessoryView property on UITextField. It is irrelevant whether your inputView is a keyboard or a picker. Set your toolbar as the inputAccessoryView and it will animate in on top of your picker with the done button and then animate out on resignFirstResponder.

geraldWilliam
  • 4,123
  • 1
  • 23
  • 35
  • I did that, im talking about my toolBar – i_duhh_bomb Aug 07 '13 at 16:50
  • Yes, I know that you mean your toolbar. As far as I can tell, you want a toolbar with a Done item on it to animate up with your picker and be presented just above it. And then disappear along with your picker. Above, I have described a solution for that; using the inputAccessoryView property of UITextField. See also the UITextField docs and @Firo's example. – geraldWilliam Aug 07 '13 at 17:27
  • When this happens I have to move the toolbar which makes it untouchable. – i_duhh_bomb Aug 07 '13 at 18:06
  • You had to move it? I don't follow you. Where did it appear? Why did you feel you had to move it? Where did you move it? What did you expect? You say you have tried this approach but that code is not in the question. How were you attempting to set the inputAccessoryView? (I know I'm asking a lot of questions. Rather than answering in the comments, please feel free to edit/update your question) – geraldWilliam Aug 07 '13 at 18:10