最近在做一個功能觀看密碼的功能
關於UITextField 右邊多一個按鈕 直接上圖
程式
BOOL isSecurePass;
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
self.secureTextEntry = YES;
self.clearButtonMode = UITextFieldViewModeNever;
UIButton *Button_ShowPassword = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[Button_ShowPassword setImage:[UIImage imageNamed:@"eye"] forState:UIControlStateNormal];
isSecurePass = YES;//預設密碼不顯示
//顯示密碼
[Button_ShowPassword setAction:@"TouchInside" withBlock:^(UIButton *response) {
isSecurePass = !isSecurePass;
self.secureTextEntry = isSecurePass;
}];
self.rightView = Button_ShowPassword;
self.rightViewMode = UITextFieldViewModeAlways;
}
- (CGRect) rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -= 10;
return textRect;
}