本文介绍了键盘上方移动文本字段的逻辑点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 现在,我有基本代码,用于在开始编辑时在键盘上方移动文本字段。但是,文本字段的大小因设备和方向而异。所以,我写了一个粗略的做法,它不会始终保持在键盘上方,而是在旋转时会进一步上升,因此看起来不像我想的那样专业。Right now, I have basic code for moving the textfield above the keyboard when you start editing. However, the size of the textfield varies based on device and orientation. So, I wrote a crude way of doing it, which doesn't stay consistently right above the keyboard, but instead will go up further when you rotate it, and so it doesn't look as professional as I would like.我的问题的基本含义是,是否有一个逻辑可以根据设备和方向获取键盘的大小,并自动使用该值并希望比这更快。The basic sense of my question is if there is a logic for getting the size of the keyboard based on device and orientation and using that value automatically and hopefully faster than this.如果这是最好的方法,请告诉我。否则,请提供意见。这是我的代码。 (这只是上移代码,而不是下移代码,以防止占用太多空间)If that is the best way, please let me know. Otherwise, please provide input. Here is the code that I have.(This is just the move-up code, not the move down code, in order to prevent taking up too much space)- (void)textFieldDidBeginEditing:(UITextField *)textField { //Get Device Type NSString *deviceType = [[UIDevice currentDevice] model]; //Animate Text Field [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:0.4]; [UIView setAnimationBeginsFromCurrentState:YES]; if ([deviceType isEqualToString:@"iPhone"]) { //Size For iPhone googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); } else if ([deviceType isEqualToString:@"iPad"]) { //Size for iPad googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 320.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); } else if ([deviceType isEqualToString:@"iPod touch"]) { //Size For iPod Touch googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); } [UIView commitAnimations];}推荐答案你是什么真正想做的是观察UIKeyboard(Did | Will)(显示|隐藏)通知。它们在userInfo词典中包含开始和结束帧,以及正确的动画曲线和持续时间。What you really want to do is observe the UIKeyboard(Did|Will)(Show|Hide) notifications. They contain in their userInfo dictionaries the beginning and ending frame, as well as the correct animation curve and durations.因此,在观察此通知后,当它被发布时移动您的文本字段根据提供的动画提示,根据通知中传递的帧的大小。So after observing this notification, when it's posted move your text field based on the size of the frame passed in the notification, according to the animation hints provided.您可以在UIWindow类引用的通知部分中看到更多信息: https://developer.apple.com/library /ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.htmlYou can see more information in the UIWindow class reference's "notifications" section: https://developer.apple.com/library/ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html下面是一个示例视图控制器实现。此视图控制器的nib只是一个文本字段,其中连接了一个插座,文本字段的委托设置为视图控制器。Below is a sample view controller implementation. The nib for this view controller was just a single text field, with an outlet connected to it, and the text field's delegate set to the view controller.@interface ViewController ()- (void)viewControllerInit;@end@implementation ViewController@synthesize textField;- (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self viewControllerInit]; } return self;}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { [self viewControllerInit]; } return self;}- (void)viewControllerInit{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - Notification Handlers- (void)keyboardWillShow:(NSNotification *)notification{ // I'll try to make my text field 20 pixels above the top of the keyboard // To do this first we need to find out where the keyboard will be. NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue]; // When we move the textField up, we want to match the animation duration and curve that // the keyboard displays. So we get those values out now NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration = [animationDurationNumber doubleValue]; NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey]; UIViewAnimationCurve animationCurve = [animationCurveNumber intValue]; // UIView's block-based animation methods anticipate not a UIVieAnimationCurve but a UIViewAnimationOptions. // We shift it according to the docs to get this curve. UIViewAnimationOptions animationOptions = animationCurve << 16; // Now we set up our animation block. [UIView animateWithDuration:animationDuration delay:0.0 options:animationOptions animations:^{ // Now we just animate the text field up an amount according to the keyboard's height, // as we mentioned above. CGRect textFieldFrame = self.textField.frame; textFieldFrame.origin.y = keyboardEndFrame.origin.y - textFieldFrame.size.height - 40; //I don't think the keyboard takes into account the status bar self.textField.frame = textFieldFrame; } completion:^(BOOL finished) {}];}- (void)keyboardWillHide:(NSNotification *)notification{ NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration = [animationDurationNumber doubleValue]; NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey]; UIViewAnimationCurve animationCurve = [animationCurveNumber intValue]; UIViewAnimationOptions animationOptions = animationCurve << 16; [UIView animateWithDuration:animationDuration delay:0.0 options:animationOptions animations:^{ self.textField.frame = CGRectMake(20, 409, 280, 31); //just some hard coded value } completion:^(BOOL finished) {}];}#pragma mark - View lifecycle- (void)viewDidUnload{ [self setTextField:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}#pragma mark - UITextFieldDelegate- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [self.textField resignFirstResponder]; return YES;}@end 这篇关于键盘上方移动文本字段的逻辑点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 06:51