UISearchBar not resigning - ios

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];
}];
}

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];
}];
}
}

How to stop iOS keyboard interfering with UITextField position

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];

Remove UIView after animation without losing other elements

What happens if I toggle between these two for hundreds of times? will be left handers of unused UIViews?
I have several controls on my main UIView and I want to animate (slide-up and slide-down) them as a group of controls, several times, then I decided to add them to an UIView and animate that view. But It seems every time when I add the view to the superView it remains there and if I run [view removeFromSuperView] it also remove all controls I've added to the view. Does it affect performance or can it cause memory leak problem?
(void)slideUp{
repeatViewTop = datePickerButton.frame.origin.y;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)];
[view addSubview:taskTitle];
[view addSubview:taskNote];
[view addSubview:datePickerButton];
[view addSubview:taskSelectedDateViewer];
[self.view addSubview:view];
[UIView animateWithDuration:0.4f
delay:0.1f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
float tempY=view.frame.origin.y;
[view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
[view setAlpha:0.4f];
}
completion:^(BOOL finished){
}];
[self.view insertSubview:view atIndex:0];
}
This will slide down those controls:
- (void)slideDown{
taskTitle.userInteractionEnabled=NO;
//150=view.frame.origin.y after sliding up
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -114, SCREEN_WIDTH, repeatViewTop)];
[view addSubview:taskTitle];
[view addSubview:taskNote];
[view addSubview:datePickerButton];
[view addSubview:taskSelectedDateViewer];
[self.view addSubview:view];
[UIView animateWithDuration:0.4f
delay:0.1f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
float tempY=view.frame.origin.y *(-1);
[view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
[view setAlpha:1.0f];
}
completion:^(BOOL finished){
}];
[self.view insertSubview:view atIndex:0];
}
You should add the elements in a View only once and keep reference to your view, then animate it and not insert it all the time.
You can create the view at any time, like in the viewDidLoad delegate, like this:
- (void)viewDidLoad {
//Other code here
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, repeatViewTop)];
view.tag = 1999;
[view addSubview:taskTitle];
[view addSubview:taskNote];
[view addSubview:datePickerButton];
[view addSubview:taskSelectedDateViewer];
[self.view addSubview:view];
}
then get a reference to the view in your animations like this:
(void)slideUp{
repeatViewTop = datePickerButton.frame.origin.y;
UIView *view = [self.view viewWithTag:1999];
[UIView animateWithDuration:0.4f
delay:0.1f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
float tempY=view.frame.origin.y;
[view setCenter:CGPointMake(SCREEN_WIDTH/2,tempY)];
[view setAlpha:0.4f];
}
completion:^(BOOL finished){
}];
}

How to add subview with flip animation?

If you create a brand new single view app and put this code behind a button:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self.view addSubview:blah];
}
completion:^(BOOL finished){
}];
The subview is added immediately with no animation. If you add the subview first then try to animate it... you get the same problem.
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:blah];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished){
}];
How on Earth do you animate a flip for a subview while or immediately after adding it?
You generally need to have the container that constrains the animation to be in place already:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 100, 100);
_container = [[UIView alloc] initWithFrame:frame];
_container.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_container];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
subview.backgroundColor = [UIColor darkGrayColor];
[UIView transitionWithView:_container
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[_container addSubview:subview];
}
completion:NULL];
}
This is worth a try:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[self.view addSubview:blah];
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block
[UIView transitionWithView:blah
duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.alpha = 1.0;
}
completion:^(BOOL finished){
}];

How to fit image in UITextView and Tap to get larger

I have created a Table-view in main ViewController once i touch the row it will take to the next view controller(DetailViewController) where it display the image and name of dish.And once i tap the image it should get larger but it is not getting bigger .The below code is which i have tried in DetailViewController.
DetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
CGRect textViewFrame = CGRectMake(10, 10, 300, 400);
textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor=[UIColor whiteColor];
textView.editable=NO;
textView.delegate = self;
[self.view addSubview:textView];
[textView addSubview:imageView];
textView.alpha = 0.9;
textView.font = [UIFont systemFontOfSize:15];
if(mrowno==0)
{
imageView.image=[UIImage imageNamed:#"Dosa.jpg"];
textView.text = #"\n\n\n\n\n\n\n\nDOSA\nDosa, ";
imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
}
Here is the method to make image larger
-(void)imageTapped:(UIButton*)in_sender
{
[UIView transitionWithView:in_sender.superview duration:0.8f options:UIViewAnimationOptionCurveLinear animations:^
{
[in_sender.superview setFrame:CGRectMake(0, 0, 300, 500)];
}
completion:^(BOOL finished)
{
}];
}
You can increase and decrease the size of image by pinch in and pinch out,If it required
-(void)viewDidLoad
{
UIPinchGestureRecognizer* pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinching:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinching:(UIGestureRecognizer*) recognizer
{
UIPinchGestureRecognizer* pinch=(UIPinchGestureRecognizer*) recognizer;
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
pinch.scale=1;
NSLog(#"pinching");
}
You do not want to use transitionWithView. Instead, you want to use + animateWithDuration: delay:options:animations:completion: (or any of its shorter variants...)
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewYouWantToAnimate.frame = CGRectMake(..., ..., ..., ...);
} completion:^{
//Add completion code here, or pass nil if you don't have anything to do.
}];
Edit: In case you were wondering what transitionWithView was used for... the AppleDocs have a great example of it adding animations for remove/addSubview methods (not frame modifications):
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView];
} completion:NULL];

Resources