现象描述:
为企业提供网站设计、成都网站制作、网站优化、全网营销推广、竞价托管、品牌运营等营销获客服务。创新互联拥有网络营销运营团队,以丰富的互联网营销经验助力企业精准获客,真正落地解决中小企业营销获客难题,做到“让获客更简单”。自创立至今,成功用技术实力解决了企业“网站建设、网络品牌塑造、网络营销”三大难题,同时降低了营销成本,提高了有效客户转化率,获得了众多企业客户的高度认可!
ios平台,app内嵌h5,当软键盘弹出再收起时,页面布局是错乱的。直接表现是当点击其他元素时,却导致了某个文本框聚焦。
解决方案:文本框focus(聚焦)时软键盘弹出,blur(失焦)时软键盘收起。可监听文本框失焦得知软键盘收起,在文本框失焦时将页面滚动距离置为0,即可解决这一问题:
KeyboardView.h 文件
@protocol KeyboardViewDelegate NSObject
@required
-(void)uploadMessage:(NSString* )text;
@end
@interface KeyboardView : UIView
// 代理
@property(nonatomic,weak)id KeyboardViewDelegate delegate;
@property(nonatomic,strong)UITextView * textView;
-(void)setTextViewPlaceholder:(NSString *)text;
@end
KeyboardView.m 文件
@interface KeyboardView ()UITextViewDelegate{
BOOLstatusTextView;
NSString* placeholderText;
}
@property(nonatomic,strong)UIButton * emptyBtn;
@property(nonatomic,strong)UIView * backGroundView;
@property(nonatomic,strong)UILabel * placeholderLabel;
@property(nonatomic,strong)UIButton * senderButton;
@end
@implementation KeyboardView
-(instancetype)initWithFrame:(CGRect)frame{
if(self== [superinitWithFrame:frame]) {
[selfcreateUI];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardhidenShow:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
-(void)createUI{
// 添加视图
[self addSubview:self.backGroundView];
self.textView.frame=CGRectMake(5,5,SCREEN_WIDTH-75,36);
self.placeholderLabel.frame=CGRectMake(10,10,SCREEN_WIDTH-65,24);
self.senderButton.frame=CGRectMake(SCREEN_WIDTH-60,5,50,36);
[self.backGroundView addSubview:self.textView];
[self.backGroundView addSubview:self.placeholderLabel];
[self.backGroundView addSubview:self.senderButton];
// 空白部分
[selfaddSubview:self.emptyBtn];
}
-(void)textViewDidChange:(UITextView*)textView{
if(textView.text.length==0) {
self.placeholderLabel.text = placeholderText;
self.senderButton.backgroundColor = [UIColor grayColor];
self.senderButton.userInteractionEnabled = NO;
}else{
self.placeholderLabel.text = @"";
self.senderButton.userInteractionEnabled = YES;
self.senderButton.backgroundColor = [UIColor redColor];
}
}
-(void)setTextViewPlaceholder:(NSString *)text{
placeholderText = text;
self.placeholderLabel.text = placeholderText;
}
-(void)keyBoardWillShow:(NSNotification*)notific{
// 修改 父视图 的 frame
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
NSDictionary* info = [notificuserInfo];
NSValue * value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRectkeyboardInfo = [valueCGRectValue];
CGFloatheight = keyboardInfo.size.height;
// 修改 操作视图 的 frame
self.backGroundView.frame = CGRectMake(0, SCREEN_HEIGHT - showHeight - height, SCREEN_WIDTH, showHeight);
self.emptyBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - showHeight - height);
}
-(void)keyBoardhidenShow:(NSNotification *)notific{
self.backGroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, showHeight);
self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 100);
}
-(void)sendBtnClick{
if(self.delegate [self.delegaterespondsToSelector:@selector(uploadMessage:)]) {
[self.delegate uploadMessage:self.textView.text];
}
self.textView.text=@"";
self.placeholderLabel.text = placeholderText;
self.senderButton.backgroundColor = [UIColor grayColor];
self.senderButton.userInteractionEnabled = NO;
[self.textView resignFirstResponder];
}
-(void)emptyBtnClick{
[self.textView resignFirstResponder];
}
-(UIView *)backGroundView{
if(!_backGroundView){
_backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 49)];
_backGroundView.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
}
return _backGroundView;
}
-(UITextView *)textView{
if(!_textView){
_textView= [[UITextViewalloc]init];
_textView.font = [UIFont systemFontOfSize:16];
_textView.delegate=self;
}
return _textView;
}
-(UILabel *)placeholderLabel{
if(!_placeholderLabel){
_placeholderLabel= [[UILabelalloc]init];
_placeholderLabel.font = [UIFont systemFontOfSize:16];
_placeholderLabel.textColor = [UIColor grayColor];
}
return _placeholderLabel;
}
-(UIButton *)senderButton{
if(!_senderButton){
_senderButton= [[UIButtonalloc]init];
[_senderButton setTitle:@"发送" forState:UIControlStateNormal];
_senderButton.backgroundColor = [UIColor grayColor];
_senderButton.userInteractionEnabled = NO;
[_senderButton addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _senderButton;
}
-(UIButton *)emptyBtn{
if(!_emptyBtn){
_emptyBtn= [[UIButtonalloc]init];
_emptyBtn.backgroundColor = [UIColor clearColor];
[_emptyBtn addTarget:self action:@selector(emptyBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _emptyBtn;
}
@end
通过注册键盘显示的消息[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
然后在消息回传方法中
- (void)keyboardWillShown:(NSNotification*)aNotification{
// 键盘信息字典
NSDictionary* info = [aNotification userInfo];
}
这个字典里的内容应该够你用了,有键盘的总高度等等
[img]UITextField
*textfield常用的取消键盘方法.1、在textfield所在的控制器中,实现UITextFieldDelegate的方法。textfield.delegate
=
self;-
(BOOL)textFieldShouldReturn:(UITextField
*)textField{
[textfield
resignFirstResponder];
return
YES;}这样,在点击键盘的return键时,就会退出键盘。[textfield
resignFirstResponder];表示textfield放弃第一响应者,键盘自然就退出了。2、假设有UIView
*someView
是textfield的父视图(不管中间是否还包含其他层的父视图),那么,只要设置[someView
endEditting:YES];那么,所有在someView上的textfield,或者textView都会结束编辑,键盘也会自动退出。以上就是常用的两种退出键盘方法。IOS软件开发,UITextField怎么取消弹出系统键盘?
当用户触击某一个view时,系统会自动指定该view为第一响应对象(first responser),(关于第一响应对象的我们估计可以把它看做一个标志位,每个贴上该标记的对象都能成为first responser,这样可能要好理解些)当某个包含了可编辑文本的view(UITextField或UITextView)成为第一响应对象后,该view会为文本输入开启一个"编辑会话"(editing session),之后该view会告知系统去开启并显示键盘,如果当前键盘处于隐藏状态,那么它会根据当前设备的旋转方向,自动调整并从application的底部以滑动动画形式出现,这通常都是我们看到并已熟知的键盘显示方式;如果当前键盘已处于显示状态,由于第一响应对象这个标记已被设置到当前的view上,所以键盘输入也被重定向到该view上了。
因为当一个view设置第一响应对象后,键盘会自动显示,通常情况下我们不用做其它任何事情。但是,有时我们可以调用可编辑文本view的becomeFirstResponder方法,来显示键盘。
如某一UITextField类的名为theTextField对象可调用以下方法:
[theTextField
becomeFirstResponder];
在可编辑view上调用该方法达到的效果,与用户触击该view的效果是一致的,首先会使该view成为第一响应对象,之后键盘被调用显示。
在弹出键盘的时候,你的输入框也应该向上位移到合适的显示位置。
比如你用的是UITextview
在代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView
里将你的文本框textView的frame的y坐标提升键盘的高度
在end时恢复textView的y坐标