防止键盘重叠 UIButton / UITextField
Posted
技术标签:
【中文标题】防止键盘重叠 UIButton / UITextField【英文标题】:Prevent keyboard from overlapping UIButton / UITextField 【发布时间】:2014-03-13 06:02:07 【问题描述】:我一直在通过以下网址阅读 Apple 提供的文本编程指南:
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
我试图防止按钮和文本字段被键盘重叠。
当我按照指南的说明粘贴清单 5-1(如下所列)中的代码时,我收到如下错误:
" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? "
和
" Property 'contentsInset' not found in 'scrollView' "
// 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;
【问题讨论】:
你真的使用滚动视图吗? 是的,我已经将 UIViewController 的内容(两个文本字段和一个按钮)嵌入到滚动视图 @GenieWanted 你的 UIScrollView 叫什么名字?您需要指定它来代替 scrollView。 我如何检查这个? @GenieWanted 您必须在 .h 文件中声明了 UIScrollView。对吗? 【参考方案1】:试试这个代码会解决你的问题,用TextField
替换TextView
-(void)textViewDidBeginEditing:(UITextView *)textView
[self animateTextFild:textView up:YES];
-(void)textViewDidEndEditing:(UITextView *)textView
[self animateTextFild:textView up:NO];
[textView resignFirstResponder];
-(void) animateTextFild:(UITextView *)textField up:(BOOL)up
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
animatedDistance = 216-(730-moveUpValue);
else
animatedDistance = 162-(520-moveUpValue);
if(animatedDistance>0)
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
myView.frame = CGRectOffset(ViewEdit.frame, 0, movement);
[UIView commitAnimations];
【讨论】:
【参考方案2】:下面的方法会对你有所帮助。
在您的 .m 文件中声明以下常量。
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
并添加以下代码
- (void)textFieldDidBeginEditing:(UITextField *)textField
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
heightFraction = 0.0;
else if (heightFraction > 1.0)
heightFraction = 1.0;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
else
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
CGRect viewFrame = self.scrollView.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
for (id navigBar in [self.view subviews])
if ([navigBar isKindOfClass:[UINavigationBar class]])
[self.view bringSubviewToFront:navigBar];
- (void)textFieldDidEndEditing:(UITextField *)textField
CGRect viewFrame = self.scrollView.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
for (id navigBar in [self.view subviews])
if ([navigBar isKindOfClass:[UINavigationBar class]])
[self.view bringSubviewToFront:navigBar];
【讨论】:
【参考方案3】:我找到的最合适的解决方案:COCOAWITHLOVE
// Keyboard correction method.
CGFloat animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
heightFraction = 0.0;
else if (heightFraction > 1.0)
heightFraction = 1.0;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
else
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
- (void)textFieldDidEndEditing:(UITextField *)textField
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
- (BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;
【讨论】:
以上是关于防止键盘重叠 UIButton / UITextField的主要内容,如果未能解决你的问题,请参考以下文章
如何在不重叠的情况下在 UIButton 中实现两个 IBAction?