Added a subview on App delegate window, textfield is in subview, startDateTxtFld will open date picker but date picker remains behind the keyboard. keyboard does not hide on resignFirstResponder or self.view eneEditing setting as true.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == startDateTxtFld )
{
startDateSelected =YES;
endDateSelected = NO;
// [referenceNmbrTxtFld resignFirstResponder];
// [startDateTxtFld becomeFirstResponder];
//textField.inputView=datePicker;
// [self.view endEditing:YES];
// [[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
// [APP_DELEGATE.window resignFirstResponder];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[UIView animateWithDuration:0.5 animations:^
{
[startDateTxtFld resignFirstResponder];
_bottomGap4DatePicker.constant=0;
datePicker.backgroundColor=[UIColor whiteColor];
// [_subVw4DtPkr becomeFirstResponder];
}];
[self.navigationController.view endEditing:YES];
}
else if (textField == endDateTxtFld)
{
startDateSelected =NO;
endDateSelected = YES;
// [self.view endEditing:YES];
// [referenceNmbrTxtFld resignFirstResponder];
// [endDateTxtFld becomeFirstResponder];
// textField.inputView=datePicker;
// [[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
// [APP_DELEGATE.window resignFirstResponder];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[UIView animateWithDuration:0.5 animations:^
{
// [endDateTxtFld resignFirstResponder];
_bottomGap4DatePicker.constant=0;
datePicker.backgroundColor=[UIColor whiteColor];
//[_subVw4DtPkr becomeFirstResponder];
}];
[self.navigationController.view endEditing:YES];
}
else
{
[textField becomeFirstResponder];
_bottomGap4DatePicker.constant=[UIScreen mainScreen].bounds.size.height +_subVw4DtPkr.frame.size.height;
_popUpVwConstraint.constant=-30;
}
}
Hide keyboard Anywhere in IOS :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
I tried all the solution, like adding
UITextFieldDelegate
tf_edit.returnKeyType = UIReturnKeyDone
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
Nothing helped.
textFieldShouldReturn was being called. but keyboard was not appearing. I read that it is going to some other textfield when I am clicking DONE button. But there was no other textfield. So instead of applying resignFirstResponder to my textfield name, I applied to the textfield which is a parameter of textFieldShouldReturn function and it worked. I am not sure if it is correct way. It worked for me.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//[tf_edit resignFirstResponder];
[textField resignFirstResponder];
return YES;
}
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { {
NSLog(#"textFieldDidBeginEditing===%#" , textField.text);
CGAffineTransform translation = CGAffineTransformIdentity;
if (textField==vacDate || textField==vacTime) {
[self.view endEditing:YES];
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
myDatePicker.hidden = NO;
return NO;
}
else
{
if (textField==vacType) {
translation = CGAffineTransformMakeTranslation(0, -60);
}
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
myDatePicker.hidden = YES;
return YES;
}
}
Related
I hope this question makes sense, I have a login screen, when I click on the one of my input fields (either Username or Password) a keyboard appears. but when that happens Everything moves in motion from left to right, so I have a big white space on the left side. What I am expecting is for everything to move from down to up instead. I really hope this makes sense. Here is my code:
- (void)animateTextField:(UITextField *) textField up: (BOOL) up;
- (void)NavButtonPressed: (UIButton*)sender;
- (void)NavButtonPressed: (UIButton*)sender
{
if([sender isEqual:navBarNextButton])
{
[self.idTextField resignFirstResponder];
[self.passwordTextField becomeFirstResponder];
currentResponder = 1;
[self animateTextField:self.passwordTextField up:NO];
[self animateTextField:self.passwordTextField up:YES];
[navBarNextButton setEnabled:NO];
[navBarPrevButton setEnabled:YES];
}
else
{
[self.passwordTextField resignFirstResponder];
[self.idTextField becomeFirstResponder];
currentResponder = 0;
[self animateTextField:self.idTextField up:NO];
[self animateTextField:self.idTextField up:YES];
[navBarNextButton setEnabled:YES];
[navBarPrevButton setEnabled:NO];
}
}
- (void)animateTextField:(UITextField *) textField up: (BOOL) up
{
int movementDistance;
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
if([textField isEqual:self.idTextField])
{
[textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-31.5f, 1024.0f, 45.0f)];
movementDistance = 110;
}
else
{
[textfieldNavigatorView setFrame:CGRectMake(0.0f, (1024.0f/2.0f)-26.5f, 1024.0f, 45.0f)];
movementDistance = 115;
}
}
else
{movementDistance = 0;}
const float movementDuration = 0.3f;
int movement=0;
if([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft)
movement = (up? -movementDistance : movementDistance);
if([self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
movement = (up? movementDistance : -movementDistance);
[UIView beginAnimations:#"keyboardtransition" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:movementDuration];
self.view.frame = CGRectOffset(self.view.frame, movement, 0);
[UIView commitAnimations];
}
If anyone could help, that would be amazing. My project Device is set to Landscape Right only.
I had same issue before, in iOS 7 and 8 it works differently. So I implemented a method for handling the issue:
- (void)keyboardHideShow:(UIView *)view shiftBy:(float)shiftAmount forState:(BOOL)state
{
float keyBoardOffset = shiftAmount;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect rect = [[[UIApplication sharedApplication] keyWindow] bounds];
if (state)
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8)
{
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
rect.origin.x -= keyBoardOffset;
}
else
{
rect.origin.x += keyBoardOffset;
}
}
else
{
rect.origin.y -= keyBoardOffset;
}
}
view.frame = rect;
[UIView commitAnimations];
}
You can call it like:
[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:true]; // For up
[self keyboardHideShow:textfieldNavigatorView shiftBy:31.5 forState:false]; // For down
I'm trying to create an unit converter app and not sure which command to implement for proceeding further. Below code would return to main screen upon hitting return key in number pad or tapping anywhere on the screen.
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
[self selectButton:nil];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch= [[event allTouches]anyObject];
if ([_distance isFirstResponder] && [touch view] != _distance)
{
[_distance resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
And This code would open a picker view:
- (IBAction)selectButton:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame=CGRectMake(0, 200, 320, 261);
[UIView commitAnimations];
pickerViewContainer.hidden=NO;
}
How could I open a picker view upon hitting return key or tapping anywhere on the screen? Kindly assist.
1- You can use Tap gesture recogniser initWithTarget:self action:#selector(handleTapFrom:)];
2- Use textfield delegate -(BOOL) textFieldShouldReturn { return true; // do stuff }
You can add UITapGestureRecognizer on you main view like this. And can utilize UITextFieldDelegate to check for return button pressed.
- (void) viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPickerView)];
[self.view addGestureRecognizer:gestureRecognizer];
[self.textField setDelegate:self];
}
//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self openPickerView];
return YES;
}
- (void)openPickerView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame=CGRectMake(0, 200, 320, 261);
[UIView commitAnimations];
pickerViewContainer.hidden=NO;
}
Is it possible to replace this gesture in iOS 6 with a custom gesture? I have a gesture to show the map in full screen by hiding the status and navigation bars and it works but it also zooms every time it is done.
Here is how I currently have my gesture implemented.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
in viewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(toggleBars:)];
tap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tap];
tap.delegate = self;
gesture method:
- (void)toggleBars:(UITapGestureRecognizer *)gesture
{
BOOL barsHidden = self.navigationController.navigationBar.hidden;
if (!barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[self hideTabBar:self.tabBarController];
}
else if (barsHidden)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self showTabBar:self.tabBarController];
}
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
methods for hiding/showing the tab bar:
- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width;
}
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
[UIView commitAnimations];
}
In Apple iOs photos app, Each picture take the full screen, but when You tap on it, navigation bar and tab bar with some menu options (like share picture) just appear and remain for a couple of secconds. How can I do that in my UIImageView ?
Add a UITapGestureRecognizer to your view and UiView for your topbar and bottom bar or what else you like and follow below code. I think This may help you.
//Write below code in ViewDidLoad
UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
[self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];
for (UIGestureRecognizer *gR in self.view.gestureRecognizers) {
gR.delegate = self;
// handleSingleTap Method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateRecognized)
{
CGRect viewRect = recognizer.view.bounds; // View bounds
CGPoint point = [recognizer locationInView:recognizer.view];
CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area
if (CGRectContainsPoint(areaRect, point)) // Single tap is inside the area
{
if ((m_CtrlViewTopBar.hidden == YES) || (m_CtrlViewBottomBar.hidden == YES))
{
[self showToolbar:m_CtrlViewTopBar];
[self showToolbar:m_CtrlViewBottomBar]; // Show
}
else
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
}
return;
}
CGRect nextPageRect = viewRect;
nextPageRect.size.width = TAP_AREA_SIZE;
nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
if (CGRectContainsPoint(nextPageRect, point)) // page++ area
{
//[self incrementPageNumber]; return;
}
CGRect prevPageRect = viewRect;
prevPageRect.size.width = TAP_AREA_SIZE;
if (CGRectContainsPoint(prevPageRect, point)) // page-- area
{
//[self decrementPageNumber]; return;
}
}
}
- (void)hideToolbar:(UIView*)view //Hide Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == NO)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.alpha = 0.0f;
}
completion:^(BOOL finished)
{
view.hidden = YES;
}
];
}
[timer invalidate];
timer=nil;
}
- (void)showToolbar:(UIView*)view //Show Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == YES)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.hidden = NO;
view.alpha = 1.0f;
}
completion:NULL
];
if (!timer) {
timer=[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(HideToolBarWithTime)
userInfo:nil
repeats:YES];
}
}
[self.view addSubview:view];
}
-(void)HideToolBarWithTime //Hide Toolbars with time
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
[timer invalidate];
timer=nil;
}
// Gesture Delegates
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
I have 4 textfields, when editing 3 of them I want the standard keyboard which works fine, but on the 4th textfield i want to display a picker wheel instead.
My code works fine when pressing on the 4th textfield directly, but if I edit textfield 1-3 and then press on number 4, the keyboard doesn't remove.
Any ideas why? and how to solve it?
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
NSLog(#"Edit country");
[self.view endEditing:YES];
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
if (pickerVisible)
{
picker.frame = CGRectOffset(picker.frame, 0, -341);
pickerVisible = FALSE;
}
else
{
picker.frame = CGRectOffset(picker.frame, 0, 341);
pickerVisible = TRUE;
}
[UIView commitAnimations];
[self removeKeyboard];
}
else
{
if (pickerVisible)
{
//Remove picker
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
picker.frame = CGRectOffset(picker.frame, 0, -341);
[UIView commitAnimations];
pickerVisible = FALSE;
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
//do nothing for now
}
else
{
[self.view endEditing:YES];
[self removeKeyboard];
}
}
- (void) removeKeyboard
{
[self.firstname resignFirstResponder];
[self.lastname resignFirstResponder];
[self.year resignFirstResponder];
[self.country resignFirstResponder];
}
In The view did load I have set the delegate.
- (void)viewDidLoad
{
NSLog(#"viewdidload SearchSwimmerView");
// Setup the text fields..
[self.firstname setDelegate:self];
[self.firstname setReturnKeyType:UIReturnKeyDone];
[self.firstname addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.lastname setDelegate:self];
[self.lastname setReturnKeyType:UIReturnKeyDone];
[self.lastname addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.year setDelegate:self];
[self.year setReturnKeyType:UIReturnKeyDone];
[self.year addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.country setDelegate:self];
}
Instead of using [self.view endEditing:YES] Use
[textField performSelector:#selector(resignFirstResponder)
withObject:nil
afterDelay:0];
I think setting inputView for 4th textfield as PickerView may do the trick.
textfield.inputView = pickerView;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==fourthtextfield)
{
return NO;
}
}