用了很多年的代码,突然发现这个输入框一出现就消失了。
#import "BaseView.h"
@interface InputView : BaseView<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *field;
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic,assign) CGFloat keyboardh;
- (void)show;
@end
#import "InputView.h"
@implementation InputView
+ (instancetype)viewSelect:(bselect )select; {
CGRect frame = CGRectMake(0, 0, k_screen_width, k_screen_height);
InputView *v = [[InputView alloc] initWithFrame:frame];
[v setSelect:select];
return v;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.isTouchEndEdit = YES;
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideShow:) name:UIKeyboardWillHideNotification object:nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 50, self.width, 50)];
view.backgroundColor = [UIColor whiteColor];
[self addSubview:view];
self.contentView = view;
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, view.width - 20, 30)];
field.backgroundColor = kGrayColorA;
field.placeholder = @"请输入";
field.delegate = self;
field.returnKeyType = UIReturnKeyDone;
field.layer.cornerRadius = 5;
field.layer.borderWidth = 0.5;
field.font = [UIFont systemFontOfSize:14];
field.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
[field setAutocorrectionType:UITextAutocorrectionTypeNo]; // 联想、校正
[field setAutocapitalizationType:UITextAutocapitalizationTypeNone]; // 首字母大写
[view addSubview:field];
self.field = field;
}
return self;
}
- (void)show {
[self.field becomeFirstResponder];
}
- (void)willShow:(NSNotification *)note {
NSDictionary *userInfo = [note userInfo];
CGRect keyBoardRect = [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.keyboardh = keyBoardRect.size.height;
NSValue *curveValue = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *durationValue = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSUInteger curve = 0; // 动画曲线
double duration = 0.0f; // 动画时间
[curveValue getValue:&curve];
[durationValue getValue:&duration];
[UIView beginAnimations:@"keyboardShow"context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:(UIViewAnimationCurve)curve];
self.contentView.transform = CGAffineTransformMakeTranslation(0, - self.keyboardh);
[UIView commitAnimations];
}
- (void)hideShow:(NSNotification *)note {
NSDictionary *userInfo = [note userInfo];
NSValue *curveValue = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *durationValue = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSUInteger curve = 0;
double duration = 0.0f;
[curveValue getValue:&curve];
[durationValue getValue:&duration];
[UIView beginAnimations:@"keyboardHide"context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:(UIViewAnimationCurve)curve];
self.contentView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
[self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:duration];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (self.select && self.field.text.length > 0) {
DLog(@"--- %@",self.field.text);
self.select(self.field.text);
}
[self.field resignFirstResponder];
return NO;
}
- (void)endEditAction {
[self.field resignFirstResponder];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
最终给输入框加一行代码即可解决。
[field setInputAccessoryView:[[UIView alloc] initWithFrame:CGRectZero]];
网友评论