我有一个UIView,它不在UIScrollView中.当键盘出现时,我想要上移我的视图.在我try 使用这个解决方案之前:How can I make a UITextField move up when the keyboard is present?.

它工作得很好.但在文本字段中插入数据后,它会将我带到另一个视图,当我回到这个页面时,它只是跳转,我看不到我的文本字段.这个问题有更好的解决方案吗?

推荐答案

使用以下代码来显示和隐藏键盘

//Declare a delegate, assign your textField to the delegate and then include these methods

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    return YES;
}


- (void)keyboardDidShow:(NSNotification *)notification
{
    // Assign new frame to your view 
    [self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.

}

-(void)keyboardDidHide:(NSNotification *)notification
{
    [self.view setFrame:CGRectMake(0,0,320,460)];
}

Ios相关问答推荐

在iOS 17脉冲符号效果不起作用的情况下制作UIBarButtonItem动画(没有UIImageView)

如何在wiftUI中管理导航栈?

我应该使用哪个UIButton发送事件在SWIFT上刷卡?

clang:错误:SDK 路径中不包含libarclite

当应用程序被杀死时是否还能获取位置?

动画文本位置

为什么这个 SwiftUI 状态在作为非绑定参数传递时没有更新?

如何从 Locale Swift 获取货币符号

SwiftUI:如何为 ScrollView 制作可拉伸(灵活)的粘性标题?

我可以在 Apple-Watch 上使用 iPhone 的摄像头扫描 SwiftUi 中的二维码(例如用于登录)吗

XCode 14 Beta 中 NavigationStack 的正文渲染问题

向左或向右移动时如何水平翻转sprite?

在 Swift 中按下返回键时在文本字段之间切换

CocoaPods 找不到 podFirebase/Core的兼容版本 | cloud_firestore, Flutter

未找到签名证书​​iOS Distribution

iOS 10 / Xcode 8 设备上的 NSLog 似乎被截断了?为什么?

直接下载Xcode模拟器

Xcode 4 + iOS 4.3:存档类型不存在打包程序

iPhone 未连接.连接 iPhone 后 Xcode 将继续

更新字段时,UITextField值已更改未触发