我正在使用UITextField接收用户输入。 然而,由于我的文本框位于nib的中间/底部,在键盘弹出时它会被隐藏。 是否有任何方法可以将其与键盘一起滑动,以便在选择期间它位于键盘顶部? 此外,由于我还使用了数字小键盘,是否有简单的方法在某个地方包含一个完成按钮?
感谢您的帮助。
我正在使用UITextField接收用户输入。 然而,由于我的文本框位于nib的中间/底部,在键盘弹出时它会被隐藏。 是否有任何方法可以将其与键盘一起滑动,以便在选择期间它位于键盘顶部? 此外,由于我还使用了数字小键盘,是否有简单的方法在某个地方包含一个完成按钮?
感谢您的帮助。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) 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-(460-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}
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];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
[UIView transitionWithView ...]
而不是[UIView beginAnimations ...]
,[UIView setAnimationBegins ...]
,[UIView setAnimationDuration ...]
和[UIView commitAnimations]
。 - DevC当键盘出现时,我喜欢使用Cocoa With Love的这个教程来滚动页面。
要隐藏数字键盘,可以在键盘上添加一个自定义的“完成”按钮或在屏幕其余区域上添加一个不可见的按钮。我使用代码做了后者,但是这个教程使用Interface Builder。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:self.view.window];
}
- (void)viewDidDisappear:(BOOL)animated {
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidHide:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height += 140;
else viewFrame.size.height += 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = NO;
}
- (void)keyboardDidShow:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height -= 140;
else viewFrame.size.height -= 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = YES; }
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect viewFrame = scrollView.frame;
if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
viewFrame.size = CGSizeMake(480,250);
else viewFrame.size = CGSizeMake(480,170);
else {//wants to change to portrait mode
if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
viewFrame.size = CGSizeMake(320,416);
else viewFrame.size = CGSizeMake(320,200);
}
scrollView.frame = viewFrame;
return YES;
}
这也适用于横向模式,但仅适用于iPhone。对于iPad,请相应更改框架设置。 textFieldShouldReturn:
方法将在按返回键时隐藏键盘。希望这有所帮助...
iOS
版本。但是被接受和其他顶级答案才是正确的方法。当时我只是试着去做一下 :D - tipycalFlow以下代码完美运行。
[textFieldFocused becomeFirstResponder];
[self.view addSubview:startView];
[textFieldFocused resignFirstResponder];
如果有人需要,我已经将ValayPatel的解决方案翻译成了Swift语言。
func animatedTextField(textField: UITextField, up: Bool){
var animatedDistance : Int = 0
let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)
switch UIDevice.currentDevice().orientation {
case .Portrait , .PortraitUpsideDown:
animatedDistance = 216 - (460 - moveUpValue - 5)
default:
animatedDistance = 162 - (320 - moveUpValue - 5)
}
if (animatedDistance > 0 ){
let movementDistance : Int = animatedDistance
let movementDuration : Float = 0.3
let movement = up ? -movementDistance : movementDistance
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(NSTimeInterval(movementDuration))
self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
UIView.commitAnimations()
}
}
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: CGFloat(movement))
。 - Neria Nachum在 iPhone 上,这种情况有点糟糕。你应该做的是要么设计你的界面,使得当键盘出现时字段可见,要么在键盘出现时将字段上移(完成后再下移)。
我建议看一些苹果应用程序,比如联系人或设置,看看它们是如何处理这个问题的。
此外,我相信 iPhone HIG 对此也有所规定。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%f",textField.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
[scrollView setContentOffset:scrollPoint animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[scrollView setContentOffset:CGPointZero animated:YES];
}
只需将UITableView设置为编辑模式,就像这样简单!
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}