ios xib相对布局布局 怎样解决键盘遮挡问题

IOS控件UITextView 实现placeholder效果和解决UITableView键盘遮挡
平时开发中老是出现多行的文本而且要有placeholder提示,就自己整理项目中部分代码以供参考、讨论,希望有更好实现方法的人多多指教。
思路:通过改变UITextView的文本颜色实现placeholder效果,通过改变UITableView的setContentOffset方法解决键盘遮挡
1、创建UITextView
textView = [[UITextViewalloc] init];
textView.font = [UIFontsystemFontOfSize:14];
textView.frame =CGRectMake(10, 0,DEVICE_WIDTH-20,56);
textView.autoresizingMask =UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleW
textView.backgroundColor = [UIColorclearColor];
textView.textColor=[UIColorcolorWithHexString:@#999999];
textView.delegate =
textView.text=@请在这里输入;
[self.view addSubview:textView];
2、UITextView delegate-
#pragma mark -----UITextView delegate-----
//开始编辑
-(void)textViewDidBeginEditing:(UITextView *)textView
[tableviewsetContentOffset:CGPointMake(0,100) animated:YES];
if ([textView.textisEqualToString:@请在这里输入]) {
textView.textColor=[UIColorcolorWithHexString:@#77FF00];
textView.text =@;
//完成编辑
-(void)textViewDidEndEditing:(UITextView *)textView
if (textView.text.length==0||[textView.textisEqualToString:@]) {
textView.textColor=[UIColorcolorWithHexString:@#999999];
textView.text=@请在这里输入;
detailAddress.text= textView.
[tableviewsetContentOffset:CGPointMake(0, 0)animated:YES];ScrollView上键盘遮挡问题 - 简书
ScrollView上键盘遮挡问题
之前做的项目中有过注册和修改个人信息的页面,因为输入的框比较多,如果要适配较小的屏幕时(比如说,3.5寸屏),不用滚动视图的话没有办法可以将所有输入框都显示在屏幕上。所以把输入框都放到了ScrollView上面(我用xib来拖入控件,而关于ScrollView的适配问题是比较麻烦的,自行百度),这里也会有键盘的遮挡问题。
关于ScrollView的键盘遮挡问题,一开始话了很多时间去改变它的Frame的高度,但是结果不如人意。后来在网上看了很多资料,发现一个简单有效的方法,特此记录下来,供以后查阅。
首先需要注册好键盘通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
接下来是两个注册方法的实现:
//显示键盘
- (void)keyboardWillShow:(NSNotification *)notification {
//获取键盘高度
CGFloat height = [self fetchKeyboardHeightWithNotification:notification];
//设置contentInset的值(一开始的默认值为(0,0,0,0))
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, height, 0);
[self.bgScrollView setContentInset:e];
//这个可以设置ScrollView上键盘的隐藏方式
//_bgScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnD
//隐藏键盘
- (void)keyboardWillHide:(NSNotification *)notification {
//将contentInset的值设回原来的默认值
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, 0, 0);
[self.bgScrollView setContentInset:e];
NSLog(@"scrollView.height = %f", self.bgScrollView.contentSize.height);
我就是我,颜色不一样的烟头...iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现,
首先是点击return和屏幕隐藏键盘
这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog/42545,他的文章写的很好,对大家的理解很有好处。
在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘。对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭它。例如,我们可以实现按下 Rerun (有时也是 Done、Research 等)键关闭键盘,或者,更人性化的,轻触背景关闭键盘。
1、首先讲一下按下Return键关闭键盘。
当按下键盘的 Return 键,会产生一个 Did End On Exit 事件,此时,我们告诉文本框要放弃控件,于是键盘就消失了。
假设,我们已经创建了一个 Single View Application ,并打开 ViewController.xib 文件,在 View 上拖上去了三个 Text Field ,然后,我们把这三个文本框映射到 ViewController.h 中,名称依次是 firstField、secondField 以及 thirdField 。如下图:
在这个基础上,实现轻触 Return 关闭键盘,步骤为:
(1)在 ViewController.h 中声明一个方法:
- (IBAction)textFiledReturnEditing:(id)
(2)在 ViewController.m 中实现这个方法:
-(IBAction)textFiledReturnEditing:(id)sender {
[sender resignFirstResponder];
所谓 First Responder 指的就是用户当前正在与之交互的控件。当用户使用键盘时,First Responder 就是这个键盘,resignFirstResponder 方法,顾名思义,就是放弃 First Responder 。
(3)让这三个文本框都映射到 textFiledReturnEditing 方法,不过此时的事件应当是 Did End On Exit ,具体操作是:
打开 Assistant Editor
,左边打开 ViewController.xib ,右边打开 ViewController.h ,在 Xcode 最右边打开 Connector Inspector ,然后在 View 中选择第一个文本框,在 Connector Inspector 中找到 Did End On Exit ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 textFiledReturnEditing 方法,如下图:
给其他两个文本框进行同样的操作。现在,已经实现了轻触 Return 键关闭键盘。
2、下面介绍更人性化的方法,轻触背景关闭键盘。
跟上面的步骤差不多,首先定义一个方法,然后实现这个方法,接下来将指定的控件映射到这个方法,并选择好所触发的事件。不同的是,这次我们要选择的控件不是上边的文本框,而是视图 View 本身。
(1)在 ViewController.h 文件中添加方法声明代码:
- (IBAction)backgroundTap:(id)
(2)在ViewController.m中实现这个方法:
- (IBAction)backgroundTap:(id)sender {
[firstField resignFirstResponder];
[secondField resignFirstResponder];
[thirdField resignFirstResponder];
需要说明的是,[firstField resignFirstResponder];表示,如果firstField有FirstResponder的话就放弃它,我们不用先判断firstField是否有,这条语句完全正确。
(3)让 View 映射到这个方法,不过事先,我们先要改变 View 的类型。
打开xib,选中 View ,打开 Identity Inspector ,在 class 中选择 UIControl :
(4)打开Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在Xcode最右边打开 Connector Inspector ,在 ViewController.xib 中选择 Control ,在 Connector Inspector 中找到 Touch Down ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 backgroundTap 方法,如下图:
好了,可以运行下看看效果了:
打开键盘之后,在背景区域点击一下,键盘就会向下收起来。
然后点评,在网上也有只写一个 backgroundTap 函数,然后将所有组件都 resignFirstResponser的方法,即 将组件的事件和屏幕的事件指向同一个函数。
这两个方法都是可以用的,但是呢,我更加倾向于使用同一个函数的方法,原因呢,原因就要牵扯到第二个方面的知识:
解决虚拟键盘挡住UITextField的方法
因为屏幕太小的缘故,一个键盘跳出来总是把输入框挡住,所以需要移动屏幕来匹配键盘
#pragma mark -
#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
//键盘输入的界面调整
//键盘的高度
float height = 216.0;
CGRect frame = self.view.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height -
[UIView beginAnimations:@"Curl"context:nil];//动画开始
[UIView setAnimationDuration:0.30];
[UIView setAnimationDelegate:self];
[self.view setFrame:frame];
[UIView commitAnimations];
-(BOOL)textFieldShouldReturn:(UITextField *)textField
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
//CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame =
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
- (void)textFieldDidBeginEditing:(UITextField *)textField
CGRect frame = textField.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height -
216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if(offset & 0)
CGRect rect = CGRectMake(0.0f, -offset,width,height);
self.view.frame =
[UIView commitAnimations];
#pragma mark -
只要在代码中加入这三个文件,然后将自身delegate
就可以实现屏幕的移动了,
但是这里经常会有屏幕移动后不能返回的问题,这里的解决方案就是
- (IBAction)backgroundTap:(id)sender {
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame =
在backgroundTap函数中添加这些代码,这样屏幕就会返回正常了。
michaellee4364人阅读
1. scrollview中的子视图有时需要更多的约束,尽管你觉得有些约束是多余的。因为scrollview的自视图所在的空间是contentview,在绘制的时候contentview是需要足够的约束条件从而确定contentsize的。
2. 为了使视图尺寸变化后能够重新布局,需要将“约束”弄出来
__weak IBOutlet NSLayoutConstraint *_addImageViewH
3. 在xib中加入自定义view,这个自定义的view可能是纯代码写的(只有.m和.h),也可能是代码加上同名的xib。当绘制这个自定义视图时调用的初始化方法为initWithCoder
-(id)initWithCoder:(NSCoder *)aDecoder
self = [super initWithCoder:aDecoder];
if (self) {
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:670750次
积分:5655
积分:5655
排名:第4471名
原创:139篇
转载:38篇
评论:57条
(1)(2)(2)(2)(1)(2)(3)(4)(8)(10)(24)(9)(4)(4)(6)(5)(7)(8)(3)(2)(2)(4)(1)(4)(18)(10)(1)(1)(4)(1)(1)(1)(1)(1)(12)(2)(1)(8)2015年10月 扩充话题大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 ios 动态修改xib布局 的文章

 

随机推荐