Sunday, June 2, 2013

UITextField UITextFieldViewModeAlways example in Objective C (iOS).


UITextField UITextFieldViewModeAlways

UITextFieldViewMode
Defines the times at which overlay views appear in a text field.

typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;

Constants
UITextFieldViewModeNever
The overlay view never appears.
UITextFieldViewModeWhileEditing
The overlay view is displayed only while text is being edited in the text field.
UITextFieldViewModeUnlessEditing
The overlay view is displayed only when text is not being edited.
UITextFieldViewModeAlways
The overlay view is always displayed.

UITextField UITextFieldViewModeAlways example.
[textFieldAddress_ setLeftView:buttonBookmark_];
[textFieldAddress_ setLeftViewMode: UITextFieldViewModeAlways];
[textFieldAddress_ setRightView:buttonReloadAndCancel_];
[textFieldAddress_ setRightViewMode: UITextFieldViewModeUnlessEditing];
textFieldAddress_.clearButtonMode = UITextFieldViewModeWhileEditing;

Example of [UITextField UITextFieldViewModeAlways].
-(void) onEditing:(id)sender {
    if(![self.text isEqualToString:@""]){
        self.rightViewMode = UITextFieldViewModeAlways;
    }else{
        self.rightViewMode = UITextFieldViewModeNever;
    }
}
- (BOOL)becomeFirstResponder{
    BOOL ret = YES ;
    ret = [super becomeFirstResponder] ;
    if( ret & ![self.text isEqualToString:@""]){
        self.rightViewMode = UITextFieldViewModeAlways;
    }else{
        self.rightViewMode = UITextFieldViewModeNever;
    }

    return ret ;
}

UITextField UITextFieldViewModeAlways example.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    UITextField *searchField = nil;
    for (UIView *subview in controller.searchBar.subviews) {
        DebugLog(@"%@",[subview description]);
        if ([subview isKindOfClass:[UITextField class]]) {
            searchField = (UITextField *)subview;
            UIImageView *clearIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ClearIcon.png"]];
            searchField.rightView = clearIconView;
            searchField.rightViewMode = UITextFieldViewModeAlways;

            [clearIconView release];
            break;
        }
    }
}

End of UITextField UITextFieldViewModeAlways example article.