ios/objective-c: How to move view when button pressed - ios

To move the view when a textfield is selected (bringing the textfield to the vertical center of the view), I am using textFieldDidBeginEditing and then when the user finishes, textFieldDidEndEditing to move the view back where it started.
This accomplishes the goal.
However, when it comes time for the user to press a submit button, a defect of the above is that the view shifts back before the VC disappears creating a jarring transition.
I would like the button to stay in the same place or at least have some control over the view in relation to the button.
Is there a way to exempt pressing the button from textFieldDidEndEditing or alternatively, is there a method similar to textFieldDidBeginEditing that applies to buttons so you can keep them in the center of the view.
This is what I am using for the textField
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
if ([sender isEqual:_username]) {
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -70., self.view.frame.size.width, self.view.frame.size.height);
}
else if([sender isEqual:_password]) {
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -90., self.view.frame.size.width, self.view.frame.size.height);
}
else if([sender isEqual:_Email]) {
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -110., self.view.frame.size.width, self.view.frame.size.height);
}
Thanks for any suggestions.

You could set a flag in your view controller when the submit button is pressed and if the flag is set not do the scrolling animation on textFieldDidEndEditing.

Depending on the desired life cycle of the textfield delegate for the textfield, you can try setting the textfield delegate for that textfield to nil as the first line of code in method called when the submit button is pressed. If you do this then the textFieldDidEndEditing delegate method will not be called for that textfield and you will not have the jarring transition.

Related

UIKeyboard Disabling Touch Events

When the keyboard is displayed in my app, no touches will work in my view. I will tap a UITextField and the keyboard will be presented. Now that it is displayed, I cannot tap on other textfields,buttons or select text even in the active textfield.
Any ideas what may cause this
Thanks.
UPDATE 1:
I have found that because I animate the view upwards a little when the keyboard is displayed this is causing a problem. I have the master, view controller view. Inside this is formContainerView which contains my two UITextFields and UIButton.
I animate formContainerView up so that my fields aren't hidden behind the keyboard. I run the following code to do this:
[UIView animateWithDuration:duration delay:0 options:animationOptionsWithCurve(curve) animations:^{
[self.formContainerView setFrame:CGRectOffset(self.formContainerView.frame, 0, -220.0f)];
}
completion:^(BOOL finished) { }];
So I just offset the view by 220 pixels. If I set this to 0, i.e. don't animate the position, all touches work. But by moving the view up, all touches fail to work.
Why?
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([txtfld_firstname isFirstResponder])
{
[txtfld_firstname resignFirstResponder];
[txtfld_secondname becomeFirstResponder];
}
return YES;
}
first set to the delegate for the textfield.
and after that write the above method when you click on the first textfield and after that press return button on the keyboard this (textfield delegate)method call and your keyboard disable.

UITextField repositions itself when resigning responder

My main controller is a UITextFieldDelegate and animates a UITextField when its editing status changes. In the storyboard the view's original position is in the centre of the screen (vertically and horizontally), when it is being edited I position it to the top of the screen. If I now complete the "Go" action from the keyboard, resign first responder on the text field, the text field pops back to its original position. I have tried retaining the frame of the text field in textFieldShouldEndEditing, but it continues to go back to its original position. How can I keep the view in the new position, even when the keyboard resigns?
I animate the view like this
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual: _toField] && !alreadyAnimated) {
[UIView beginAnimations:#"editFieldAnimation" context:nil];
[UIView setAnimationDuration:0.2f];
// move up to field
CGRect textFieldFrame = [_toField frame];
textFieldFrame.origin.y = 58; // default frame top margin
_toField.frame = textFieldFrame;
[UIView commitAnimations];
alreadyAnimated = YES;
}
return YES;
}

Toggle Searchbar View using UIScrollView

I have a search bar embedded above a tableview which is visible as soon as the view is opened i want it to be hidden when the page loads and on click of the search button (Bottom left corner) i want the searchbar to get animated from top. I have read that this can be done using UIScrollview, but none of the tutorials have been able to help me.
For
1)I want it to be hidden when the page loads and on click of the search button
In your search button clicked...
if(self.searchBar.isHidden)
{
[sender setSelected:YES];
self.searchBar.hidden=NO;
[self resetFrame:tableView toFrame:CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y+44, tableView.frame.size.width, tableView.frame.size.height) withDelay:0.5];
}
else if(!self.searchBar.isHidden)
{
[sender setSelected:NO];
self.searchBar.hidden=YES;
[self resetFrame:tableView toFrame:CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y-44, tableView.frame.size.width, tableView.frame.size.height) withDelay:0.5];
[self.searchBar resignFirstResponder];
}
-(void)resetFrame:(UIView *)view toFrame:(CGRect )frame withDelay:(float)delay
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:delay];
[view setFrame:frame];
[UIView commitAnimations];
}
}
Just add the above when you click search button or if want it to be hidden when page shows add it in the viewWillAppear method
2)i want the searchbar to get animated from top.
Add the searchbar to the view by making its y axis below 0 example -100 and once page loads set its actual frame use resetFrame method to bring your search bar from top

Adding [myUITextField becomeFirstResponder]; does not bring up keyboard

I've created a screen that has a UITextField on it. When I get a EditingDidBegin event, I resignFirstResponder, Bring up a Popover with another textField in it and for that TextField call the BecomeFirstResponder on it.
When it runs, I get Blinking insertion pointer and the X clear contents. Though no Keyboard. The Master UIView is set to UserInteractionEnabled:YES.
target action for First UITextField, its on its own view.
[textField addTarget:self action:#selector(wantsToEditValue:) forControlEvents:UIControlEventEditingDidBegin];
Target Action selector:
- (IBAction)wantsToEditValue:(id)sender {
// set notification so we can update from popover
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(saWriteValue:)
name:kRefreshFromPopover object:nil];
//we dont want the TagValue textfield to really be the first responder.
[textField resignFirstResponder];
[... setup popoverVC, and View. present popover...]
}
Here is the code to create the 2nd UITextField. This code is in the VC for the Popover..
- (void)viewDidLoad
{
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(#"%# - %#", [self description], NSStringFromSelector(_cmd) );
} [super viewDidLoad];
[self createElementInputControl];
[self createWriteButton];
//We want the input Focus
[textFieldInput becomeFirstResponder];
//Resize our view to handle the new width
CGRect newViewSize = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
writeButton.frame.origin.x + writeButton.frame.size.width + kWriteElementOffset ,
self.view.frame.size.height);
[self.view setFrame:newViewSize];
}
Create Input Code:
-(void) createElementInputControl {
textFieldInput = [[UITextField alloc] initWithFrame:CGRectMake( kWriteElementOffset ,
kWriteElementHeightOffset,
kTagValueInputInitialWidth,
kWriteElementDefaultHeight)];
textFieldInput.borderStyle = UITextBorderStyleRoundedRect;
textFieldInput.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldInput.textAlignment = UITextAlignmentLeft;
[textFieldInput setDelegate:self];
[textFieldInput setKeyboardType:UIKeyboardTypeDefault];
// Set the value of the text
[textFieldInput setText:self.myTag.value];
CGSize textFieldInputSize = [textFieldInput.text sizeWithFont:textFieldInput.font];
//Set the Button Width
[textFieldInput setFrame:CGRectMake(textFieldInput.frame.origin.x, textFieldInput.frame.origin.y, textFieldInputSize.width + kTagValueInputWidthBuffer, textFieldInput.frame.size.height)];
[self.view addSubview:textFieldInput];
}
When I remove the becomeFirstResponder code, the Popover come up as normal, though no blinking insertion pointer. I tap the field, I get Insertion Pointer, X clear content button, and yes a Keyboard.
I want the keyboard to show without having to click in the new Text Field.
Thanks!
In order to become a first responder, the view must be in the view hierarchy.
You need to add your textFieldInput as a subview to something.
As per Apple's doc in UIResponder:
You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.
When you call the become first responder from the didBeginEditing you will run into an infinite loop.Reason being, when you call becomeFirstResponder it calls didBeginEditing. So It Explains the cursor blink and your statement
When I remove the becomeFirstResponder code, the Popover come up as
normal, though no blinking insertion pointer. I tap the field, I get
Insertion Pointer, X clear content button, and yes a Keyboard.
To solve your problem,
In the beginEditingMethod ,
if(texfield.tag == firstTextFieldTag)
{
//Create second TextField and make it become first responder
}
else
{
// do want you want in the beginEditing of your second textfield.
}

How to present datepicker only upon click of a button

I need to know if a date picker can be presented like an alert view upon the click of a button. So far, I have hidden the date picker in viewdidload method and set hidden=no in the method for the button.
I intend to store the date within Core Data.
Create the pickerView, place it outside of your view bounds, add it as a subview and use animation to slide it up. Something like the below should work...
CGRect frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 215);
UIPickerView *typePicker = [[UIPickerView alloc] initWithFrame:frame];
[self.view addSubview:typePicker];
[UIView beginAnimations:#"slideIn" context:nil];
[typePicker setCenter:CGPointMake(typePicker.center.x, self.view.frame.size.height - typePicker.frame.size.height/2)];
[UIView commitAnimations];
You put the date picker in a subview and then present the subview at the click of a button. You probably want the subview to slide in from the side or bottom.

Resources