Wednesday 28 May 2014

Textfield hide when keyboard open

//write this code in viewdidload method

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:self.view.window];

    keyboardIsShown = NO;


- (void)keyboardWillHide:(NSNotification *)n
{
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    
    // resize the scrollview
    CGRect viewFrame = scrView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height += (keyboardSize.height - 100);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [scrView setFrame:viewFrame];
    [UIView commitAnimations];
    
    keyboardIsShown = NO;
}

- (void)keyboardWillShow:(NSNotification *)n
{
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the `UIScrollView` if the keyboard is already shown.  This can happen if the user, after fixing editing a `UITextField`, scrolls the resized `UIScrollView` to another `UITextField` and attempts to edit the next `UITextField`.  If we were to resize the `UIScrollView` again, it would be disastrous.  NOTE: The keyboard notification will fire even when the keyboard is already shown.
    if (keyboardIsShown) {
        return;
    }
    
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    // resize the noteView
    CGRect viewFrame = scrView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height -= (keyboardSize.height - 100);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [scrView setFrame:viewFrame];
    [UIView commitAnimations];
    [scrView scrollRectToVisible:activeField.frame animated:YES];
    keyboardIsShown = YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    activeField = textField;
    return YES;
}

Textfield text search on tableview


txtSearch.returnKeyType = UIReturnKeyDone;
    txtSearch.autocorrectionType = UITextAutocorrectionTypeNo;
    [txtSearch addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


-(void)textFieldDidChange:(UITextField *)txtFld {
    NSString * match = txtFld.text;
    NSArray *listFiles = [[NSMutableArray allocinit];
    NSPredicate *sPredicate = [NSPredicate predicateWithFormat:
                               @"SELF CONTAINS[cd] %@", match];
    
    listFiles = [NSArray arrayWithArray:[[arrGetTask valueForKey:@"task_name"]
                                         filteredArrayUsingPredicate:sPredicate]];
    // Now if you want to sort search results Array
    //Sorting NSArray having NSString as objects
    NSString *strFilter = [match substringFromIndex:0];
    NSLog(@"%@",strFilter);
    if ([strFilter isEqualToString:@"#"]) {
        arrFilter = nil;
    }
    else{
        arrFilter = [[NSMutableArray alloc]initWithArray: [listFiles
                                                           sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
    }
    
   
    
    NSLog(@"%@",arrFilter);
    
    [tblView reloadData];
    //Use sorted array as your Table’s data source
   // -make your table unhidden: yourTbl.hidden = FALSE;
   // - reload your table  : [yourTbl reloadData];

}