我尝试使用来自ios开发人员库https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html的代码实现它
但我不知道他们的意思是“活动字段存储在自定义变量中(在本例中称为activeFIEld)”并且我可能做了其他错误.在vIEwWillAppear中使用registerForKeyboardNotifications是好的吗?
我知道有一些关于这个问题的线索,但我是新手,我很难理解它们.而且我不想只是学习如何,但为什么,这就是为什么我不想使用github其他建议等的现成的解决方案.
我的代码atm:
.H:
#import <UIKit/UIKit.h>@interface VNVIEwController : UIVIEwController<uiscrollviewdelegate,UITextFIEldDelegate>@property (weak,nonatomic) IBOutlet UITextFIEld *texticek;@property (strong,nonatomic) IBOutlet UIScrollVIEw *scrollVIEw;@end
.M:
#import "VNVIEwController.h"@interface VNVIEwController ()@end@implementation VNVIEwController@synthesize scrollVIEw;@synthesize texticek;- (voID) vIEwWillAppear:(BOol)animated{ [super vIEwWillAppear:animated]; [self registerForKeyboardNotifications];}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw,typically from a nib.}- (voID)dIDReceiveMemoryWarning{ [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated.}// Call this method somewhere in your vIEw controller setup code.- (voID)registerForKeyboardNotifications{ [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(keyboarDWasShown:) name:UIKeyboardDIDShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(keyboarDWillBeHIDden:) name:UIKeyboarDWillHIDeNotification object:nil];}// Called when the UIKeyboardDIDShowNotification is sent.- (voID)keyboarDWasShown:(NSNotification*)aNotification{ NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0,0.0,kbSize.height,0.0); scrollVIEw.contentInset = contentInsets; scrollVIEw.scrollindicatorInsets = contentInsets; // If active text fIEld is hIDden by keyboard,scroll it so it's visible // Your app might not need or want this behavior. CGRect aRect = self.vIEw.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect,activeFIEld.frame.origin) ) { [self.scrollVIEw scrollRectToVisible:activeFIEld.frame animated:YES]; }}// Called when the UIKeyboarDWillHIDeNotification is sent- (voID)keyboarDWillBeHIDden:(NSNotification*)aNotification{ UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollVIEw.contentInset = contentInsets; scrollVIEw.scrollindicatorInsets = contentInsets;}- (voID)textFIEldDIDBeginEditing:(UITextFIEld *)textFIEld{ activeFIEld = textFIEld;}- (voID)textFIEldDIDEndEditing:(UITextFIEld *)textFIEld{ activeFIEld = nil;}@end解决方法 单击时向上移动文本字段,请使用下面的代码.它只需要你的卷轴的插座
- (voID)textFIEldDIDBeginEditing:(UITextFIEld *)textFIEld { self.scroll.contentOffset = CGPointMake(0,textFIEld.frame.origin.y);}
您可以通过减去y位置值来更改应显示的文本字段的位置(textfIEld.frame.origin.y – 某个值)
如果要为滚动设置动画,可以这样做:
CGPoint newOffset = CGPointMake(0,textFIEld.frame.origin.y-40);[self.scroll setContentOffset: newOffset animated: YES];总结
以上是内存溢出为你收集整理的当键盘在文本字段上时,ios移动scrollview全部内容,希望文章能够帮你解决当键盘在文本字段上时,ios移动scrollview所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)