How to stop iOS keyboard interfering with UITextField position - ios

I have a problem where the keyboard dismissal, I think interferes with the animation code I have. What I would like to happen is if the user clicks the search button while the keyboard is up, the keyboard drops and the text field will animate to the top of the screen.
Like in this image.
http://imgur.com/5dQ6aPZ
However, what actually happens if the search button is pressed while the keyboard is up, is this.
http://imgur.com/tp6Ffne
Here is the code for the Search button:
- (IBAction)searchButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
[UITextField commitAnimations];
}
Search Again button
- (IBAction)searchAgainButton:(id)sender {
[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
duration:0.6
options:UIViewAnimationOptionTransitionCrossDissolve
animations:NULL
completion:NULL];
_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];
[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];
_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}
And here are the methods I'm using to dismiss the keyboard
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}
-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}
I'm just trying to find the best solution to solve this problem or perhaps re-design to something similar.
Any Help would be greatly appreciated! :)

Replace your code as follows:
Hope it will work now
Search button:
- (IBAction)searchButton:(id)sender {
//[UITextField commitAnimations];
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = YES;
[_srcButton setHidden:YES];
[_srcAgainButton setHidden:NO];
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
}];
}];
}
Search Again button
- (IBAction)searchAgainButton:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
_enterRoomLabel.hidden = NO;
[_srcAgainButton setHidden:YES];
_roomCodeInput.text=Nil;
_roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
} completion:^(BOOL finished) {
// when animation
[UIView animateWithDuration:0.5 animations:^{
[_srcButton setHidden:NO];
}];
}];

NSArray * vertConstraint;
UIButton * button;
UITextField * textField;
-(void)search{
[self.view removeConstraints:vertConstraint];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
NSArray * newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-5-[textField(==30)]-(>=10)-[button]-10-|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:newVertConstraint];
[textField resignFirstResponder];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
self.automaticallyAdjustsScrollViewInsets = NO;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(search) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Search" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
textField = [[UITextField alloc] init];
textField.layer.borderWidth = 5;
textField.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:textField];
[textField setTranslatesAutoresizingMaskIntoConstraints: NO];
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(button, textField);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[textField]-20-|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-20-[button]-20-|" options:0 metrics: 0 views:viewsDictionary]];
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-100-[textField(==30)]-10-[button]" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[textField becomeFirstResponder];

Related

How to remove view from superview in run time?

Based on internet connectivity I have to remove or add subview to superview.
I can able to add subview in runtime. But not remove from subview.
I tried like this
if ([statusString isEqualToString:#"Access Not Available"]){
view = [[UIView alloc]initWithFrame:CGRectMake(0, navigationView.frame.size.height, self.view.frame.size.width, 50)];
[self.view addSubview:view];
view.backgroundColor = [UIColor lightGrayColor];
}else{
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[view removeFromSuperview];
}];
}
But it is not removing from superview.
How can I do this?
///in view did load
view_NoConnectn = [[UIView alloc]init];
[view_NoConnectn setBackgroundColor:[UIColor whiteColor]];
[view_NoConnectn setFrame:CGRectMake(0, frameHeight, frameWidth, 35)];
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
bool isFound=false;
for(UIView *child in [mainWindow subviews])
{
if([child tag]==007)
isFound=true;
}
if(!isFound)
{
[mainWindow addSubview: btn_setting];
}
[self.navigationController.view addSubview:view_NoConnectn];
////// whereever required
if (show){
if (self.navigationController.view.frame.size.height == frameHeight) {
[UIView animateWithDuration:1.0 animations:^{
[self.navigationController.view setFrame:CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y, self.navigationController.view.frame.size.width, frameHeight - 35)];
[view_NoConnectn setFrame:CGRectMake(0, frameHeight-35, frameWidth, 35)];
[self.view layoutIfNeeded];
}];
}
}
else
{
if (self.navigationController.view.frame.size.height != frameHeight) {
[UIView animateWithDuration:1.0 animations:^{
[UIView animateWithDuration:0.8 animations:^{
[self.navigationController.view setFrame:CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y, self.navigationController.view.frame.size.width, frameHeight)];
[view_NoConnectn setFrame:CGRectMake(0, frameHeight, frameWidth, 35)];
[self.view layoutIfNeeded];
}];
} completion:^(BOOL finished) {
[view_NoConnectn removeFromSuperview];
}];
}
}

Pull down menu from Navigation bar

Does anyone know of a good solution to attaching a retractable menu to the bottom of the navigation bar?
I have found this: https://github.com/mentionapp/mntpulltoreact, but this has the behavior of pulling down on the whole view, not letting go, and then selecting the item that one wants by dragging your finger along.
The Zappos app has a perfect example of what I am looking for. Does anyone know what they could have been using or an existing solution for this? I've searched on cocoacontrols and haven't found much.
Would doing something like this work for you? then of course you can just add whatever menu buttons you need to the menu view.
UIView * menu;
UIButton * activate;
int menuHeight;
int buttonHeight;
-(void)toggleMenu:(UIButton*)sender{
[sender setUserInteractionEnabled:NO];
[self.view bringSubviewToFront:menu];
if (sender.tag == 1000){
[UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y - menu.frame.size.height + buttonHeight ,menu.frame.size.width,menu.frame.size.height);
menu.frame = frame;
[sender setUserInteractionEnabled:YES];
} completion:nil];
sender.tag = 2000;
} else {
[UIView transitionWithView:menu duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = CGRectMake(menu.frame.origin.x,menu.frame.origin.y + menu.frame.size.height - buttonHeight ,menu.frame.size.width,menu.frame.size.height);
menu.frame = frame;
[sender setUserInteractionEnabled:YES];
} completion:nil];
sender.tag = 1000;
}
}
menuHeight = 100;
buttonHeight = 20;
activate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[activate addTarget:self action:#selector(toggleMenu:) forControlEvents:UIControlEventTouchUpInside];
[activate setTitle:#"Menu" forState:UIControlStateNormal];
activate.frame = CGRectMake(80.0, menuHeight - buttonHeight, 70.0, buttonHeight);
activate.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
activate.tag = 2000;
menu = [[UIView alloc] initWithFrame:CGRectMake(0,0 - menuHeight + buttonHeight,self.view.bounds.size.width, menuHeight)];
menu.autoresizingMask = UIViewAutoresizingFlexibleWidth;
menu.backgroundColor = [UIColor grayColor];
[self.view addSubview:menu];
[menu addSubview:activate];
you can then use some constraints to shift the table down and what not. this is untested but should get you pointed in the right direction
-(void)toggleMenu:(UIButton*)sender{
UIView * menu;
[sender setUserInteractionEnabled:NO];
[self.view bringSubviewToFront:menu];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary;
viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);
NSArray * newVertConstraint;
if (sender.tag == 1000){
newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", 0.0] options:0 metrics: 0 views:viewsDictionary];
sender.tag = 2000;
} else {
newVertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|[bottomGuide][menu(==%f)][myTable][bottomGuide]|", menu.frame.size.height] options:0 metrics: 0 views:viewsDictionary];
sender.tag = 1000;
}
[self.view removeConstraints:vertConstraint];
[self.view addConstraints:newVertConstraint];
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.view layoutIfNeeded];
}];
vertConstraint = newVertConstraint;
}
NSArray * vertConstraint;
UITableView * myTable = [[UITableView alloc] init];
[self.view addSubview:myTable];
self.automaticallyAdjustsScrollViewInsets = NO;
[myTable setTranslatesAutoresizingMaskIntoConstraints: NO];
[menu setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary * viewsDictionary;
viewsDictionary = NSDictionaryOfVariableBindings(myTable, topGuide, bottomGuide, menu);
vertConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[bottomGuide][menu(==0)][myTable][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary];
[self.view addConstraints:vertConstraint];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[menu]|" options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[myTable]|" options:0 metrics: 0 views:viewsDictionary]];

UIButton show and disappear with animation

I would like to create a button that will show for only 5 second and then disappear. User have to click the button before it disappear. So I use animationWithDuration to set alpha to 1 and then set it back to 0. Here is my code...
__block UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
showButton.alpha = 1.0;
}
completion:nil];
[UIView animateWithDuration:0.5
delay:3.0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton removeFromSuperview];
showButton = nil;
}];
-(void)showButtonClick{
NSLog(#"click")
}
But the showButtonClick can't get called. What am I do wrong?
Try this code:
UIButton *showButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
[showButton setTitle:#"go to next page!" forState:UIControlStateNormal];
showButton.alpha = 0;
[showButton addTarget:self action:#selector(showButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showButton];
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[showButton setHidden:NO];
showButton.alpha = 1.0;
}
completion: {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction
animations:^{
showButton.alpha = 0.0;
}
completion:^(BOOL finished){
[showButton setHidden:YES];
}];
}];
-(void)showButtonClick{
NSLog(#"click")
}
You should create a NSTimer or something else to call the animation. Because both of your animation are being called at the same time. That is why you were not able to click the button, thus showButtonClick wasn't being call.
[UIView animateWithDuration:5.0
animations:^{
initWithFrame:CGRectMake(100, 0, 150, 40)
//Animation code goes here
} completion:^(BOOL finished) {
//Code to run once the animation is completed goes here
[your button remove from removefromsuperview];
}];
I can't understand,why you are using animation two times.....as your button is frame is allocated as initWithFrame:CGRectMake(0, 0, 150, 40).....check the animation code above....it will animate the button from 0 to 100....after animation completed the button will be removed...

UISearchBar not resigning

I have a searchBar to which I set the delegate = self and to the delegate method "endEditing" wrote some actions. On the didBeginEditing I set a touchGesture to which acts as a background touch to resign the searchBar outlet. On the touch action i resign the searchBar's responder but for some reason it ends editing instead. I can tell that because it performs the actions added to the didEndEditing. What am I missing? thank you in advance..
-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
[_searchBarOut becomeFirstResponder];
UIView *view1 = [[UIView alloc] initWithFrame:self.view.bounds];
[view1 setBackgroundColor:[UIColor colorWithWhite:.5 alpha:.5]];
[view1 setTag:20];
[view1 addGestureRecognizer:searchTap];
[view1 setAlpha:0];
[self.view addSubview:view1];
[self.view bringSubviewToFront:_searchBarOut];
[UIView animateWithDuration:.3 animations:^{
[view1 setAlpha:1];
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 100, self.view.frame.size.width, self.view.frame.size.height)];
} completion:^(BOOL finished) {
}
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar endEditing:YES];
}
-(void) searchTap:(id) sender{
[_searchBarOut resignFirstResponder];
UIView *view1 = (UIView *)[self.view viewWithTag:20];
if ([_searchBarOut isFirstResponder]){
[UIView animateWithDuration:.3 animations:^{
[view1 setAlpha:0];
} completion:^(BOOL finished) {
[view1 removeFromSuperview];
}];
}
}
-(void) searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[_searchBarOut resignFirstResponder];
UIView *view1 = [(UIView *) self.view viewWithTag:20];
[UIView animateWithDuration:.3 animations:^{
[view1 setAlpha:0];
[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 100, self.view.frame.size.width, self.view.frame.size.height)];
} completion:^(BOOL finished) {
[view1 removeFromSuperview];
[self pushWithString:searchBar.text];
}];
}

ios Animation with [removeFromSuperview]

I use the following lines of code:
[UIView animateWithDuration:0.50
animations:^{ itemView.transform = CGAffineTransformIdentity; }
completion:^(BOOL finished) { if (finished) [itemView removeFromSuperview]; }];
The animation does nothing. If I take the removeFromSuperview out of the completion block, it doesn't work either, I guess because the view is removed before it completes. So, either way, the appearance is the same - no animation.
I'll appreciate any suggestion or workaround on this.
Are you setting the transform correctly? The below is an example of scaling a UIView to zero area and then removing it from the superview.
UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[itemView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:itemView];
CGAffineTransform trans = CGAffineTransformScale(self.view.transform, 0, 0);
[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
itemView.transform = trans;
} completion:^(BOOL finished) {
[itemView removeFromSuperview];
}];

Resources