gpt4 book ai didi

ios 当键盘位于文本字段上时移动 ScrollView

转载 作者:可可西里 更新时间:2023-11-01 03:22:08 25 4
gpt4 key购买 nike

拜托,你能更正我的代码吗,所以我有一个简单的应用程序示例,当键盘位于其上方时,文本字段会向上移动?

我尝试使用来自 ios 开发人员库的代码来实现它 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

但我不知道“事件字段存储在自定义变量中(在本例中称为 activeField)”是什么意思,我可能做错了什么。在 viewWillAppear 中使用 registerForKeyboardNotifications 可以吗?

我知道有一些关于这个问题的线索,但我是新手,很难理解它们。而且我不想只是学习如何,而是为什么,这就是为什么我不想使用其他人建议的 github 中现成的解决方案等。

我的自动取款机代码:

.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 ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com