viernes, 20 de mayo de 2016

iOS: UITextField with image on its right

Interface

IB_DESIGNABLE

@interface MyDropDownTextField : MyTextField

@property (nonatomic, strong) IBInspectable UIImage* rightViewImage;


@end

Implementation

#import "MyDropDownTextField.h"

@implementation MyDropDownTextField

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    self.rightViewMode = UITextFieldViewModeAlways;
    self.rightView = [[UIImageView alloc] initWithImage:self.rightViewImage];

    CGFloat height = self.frame.size.height;
    CGFloat y = (height - 6)/2;
    self.rightView.frame = CGRectMake(self.rightView.frame.origin.x, y, 9, 6);
}

- (CGRect) rightViewRectForBounds:(CGRect)bounds {
    
    CGRect textRect = [super rightViewRectForBounds:bounds];
    textRect.origin.x -= 10;
    return textRect;
}

@end

No hay comentarios:

Publicar un comentario