Sunday, June 2, 2013

UITextField rightView example in Objective C (iOS).


UITextField rightView

The overlay view displayed on the right side of the text field.

@property(nonatomic, retain) UIView *rightView

Discussion of [UITextField rightView]
You can use the right overlay view to provide indicate additional features available for the text field. For example, you might display a bookmarks button in this location to allow the user to select from a set of predefined items.

The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit.[UITextField rightView]

If your overlay view does not overlap any other sibling views, it receives touch events like any other view. If you specify a control for your view, that control tracks and sends actions as usual. If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events. By default, the right overlay view does overlap the clear button.

UITextField rightView example.
//add button to address bar
//UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)]
[refreshButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[refreshButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateHighlighted];
refreshButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = refreshButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;

Example of [UITextField rightView].
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@\"RefreshIcon.png\"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@\"RefreshIcon.png\"] forState:UIControlStateHighlighted];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = button;
addressBar.rightViewMode = UITextFieldViewModeAlways;

UITextField rightView example.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    ...
    [self addTarget:self action:@selector(onEditing:) forControlEvents: UIControlEventEditingChanged]
    ...
}

-(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 ;
}
- (BOOL)resignFirstResponder
{
    BOOL ret = YES ;
    ret = [super resignFirstResponder] ;
    if( ret )
        self.rightViewMode = UITextFieldViewModeNever;
    return ret ;
}
- (void) clearText:(id)sender
{
    self.text = @"";
    self.rightViewMode = UITextFieldViewModeNever;
}

End of UITextField rightView example article.