解決鍵盤覆蓋TextField的問題

//移除觀察者身份

- (void)dealloc{

[[NSNotificationCenter? defaultCenter]? removeObserver: self];

[super dealloc];

}

- (void)viewDidLoad{

[super viewDidLoad];

[self createTextField];

[self createkeyboardMonitor];

}

//將textField作為鍵盤的附加視圖

- (void)createTextField{

CGSize size = self.view.frame.size;

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50,size.height - 100,size.width - 100,50)];

//對輸入的內(nèi)容進(jìn)行加密

tf.secureTextEntry = YES;

//記錄原始frame

_originalFrame = tf.frame;

//定制附加視圖

[self customInputAccessoryViewFor:tf];

//定制主鍵盤

UIView *keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0,0,size.width,size.height / 2)];

keyboardView.backgroundColor = [UIcolor yellowColor];

tf.inputView = keyboardView;

[keyboardView release];

tf.borderStyle = UITextBorderStyleBezel;

tf.tag = 100;

[self.view addsubView:tf];

[tf release];

}

- (void)customInputAccessoryViewFor:(UITextField *)tf{

CGFloat width = self.view.frame.size.width;

UIView *whiteView = [[UIview alloc] initWithFrame:CGRectMake(0.0,width,50)];

whiteView.backgroundColor = [UIcolor whiteColor];

NSArray *titles = @[@"??",@"??",@"??",@"??",@"??",@"??",@"??",@"??",@"??"];

CGFloat buttonwidth = width/titles.count;

CGFloat buttonHeight = whiteView.frame.size.height;

for (NSUInteger i = 0;i < titles.count;i++){

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

button.frame = CGRectMake(i * buttonWidth,0,buttonWidth,buttonHeight);

[button setTitle:titles[i] forState:UIControlStateNormal];

button.titleLabel.font = [UIFont systemFontOfSize:40];

[button addTarget:self action:@selector(clickHandle:) forControlEvents:UIControlEventTouchUpInside];

[whiteView addSubview:button];

}

tf.inputAccessoryView = whiteView;

[whiteView release];

}

- (void) clickHandle:(UIButton *)button{

UITextField *tf = [self.view viewWithTag:100];

NSString *content = [tf.text stringByAppendingString:button.currentTitle];

tf.text = content;

}

//注冊第一響應(yīng)者

- (void)touchesBegan:(NSSet<UITouch *> *) withEvent:(UIEvent *)event{

[super touchesBegan:touches withEvent:event];

UITextField *tf = [self.view viewWithTag:100];

[tf resignFirstResponder];

}

//注冊觀察者身份

- (void) keyboardMonitor{

[[NSNotificationCenter defaultCenter] addObserver:self? selector:@selector(moveTextField:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moveTextField:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)moveTextField:(NSNotification *)notificcation{

UITextField *tf = [self.view viewWithTag:100];

if ([notification.name isEqualToString:UIKeyboardWillShowNotification
]){

NSValue *rectValue = notification.userInfo[UIKeyboardFrameBeginUserInfoKey];

CGrect rect;

[rectValue getValue:&rect];

//將textField加在keyboard上面

CGPoint center = tf.center;

center.y - = rect.size.height;

tf.center = center;

}else{

tf.frame = _orignalFrame;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容