多个文本字段的滚动视图不起作用

Posted

技术标签:

【中文标题】多个文本字段的滚动视图不起作用【英文标题】:Scroll view for multiple textFields wont work 【发布时间】:2013-05-03 09:41:10 【问题描述】:

嗨,我可以很容易地滚动处理一个文本框,但是当我添加 10 个文本框并使用苹果文档中的代码时,我无法弄清楚如何让它将 10 个字段中的任何一个字段的触摸委托给滚动视图,我无法弄清楚如何将 activeField 连接到相关的 textField。我认为这就是我失败的地方,答案在于授权

@interface ImmyViewController ()

@end

@implementation ImmyViewController
@synthesize activeField;
@synthesize scrollView;
@synthesize text1;
@synthesize text2;
@synthesize text3;
@synthesize text4; 
@synthesize text5;
@synthesize text6;
@synthesize text7;
@synthesize text8;
@synthesize text9;
@synthesize text10;

- (void)viewDidLoad

    [super viewDidLoad];

    text1.delegate =self;
    text2.delegate =self;
    text3.delegate =self;
    text4.delegate =self;
    text5.delegate =self;
    text6.delegate =self;
    text7.delegate =self;
    text8.delegate =self;
    text9.delegate =self;
    activeField.delegate=self;

    text10.delegate =self;

// Do any additional setup after loading the view, typically from a nib.
//---set the viewable frame of the scroll view---
    scrollView.frame = CGRectMake(0, 0, 320, 460);

//---set the content size of the scroll view---
    [scrollView setContentSize:CGSizeMake(320, 833)];


// 在视图控制器设置代码中的某处调用此方法。 - (void)registerForKeyboardNotifications [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(keyboardWasShown:) 名称:UIKeyboardDidShowNotification 对象:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];

// 在发送 UIKeyboardDidShowNotification 时调用。 - (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 application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) 
    CGPoint scrollPoint = CGPointMake(0.0, text10.frame.origin.y-kbSize.height);
    [scrollView setContentOffset:scrollPoint animated:YES];

// UIKeyboardWillHideNotification 发送时调用 - (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;

【问题讨论】:

【参考方案1】:

您可以使用 textFieldDidBeginEditing 来管理所有文本字段,只需在此代码中替换您的值即可。

- (void)textFieldDidBeginEditing:(UITextField *)textField
    if (textField.tag==1) 
        [scroll_view setContentOffset:CGPointMake(0, 0)animated:YES];
    
    if (textField.tag==2) 
        [scroll_view setContentOffset:CGPointMake(0, 81)animated:YES]; 
    
    if (textField.tag ==3) 
        [scroll_view setContentOffset:CGPointMake(0, 115)animated:YES]; 
    
    if (textField.tag ==4) 
        [scroll_view setContentOffset:CGPointMake(0, 150)animated:YES]; 
    
    if (textField.tag ==5) 
        [scroll_view setContentOffset:CGPointMake(0, 185)animated:YES]; 
    
    if (textField.tag ==6) 
        [scroll_view setContentOffset:CGPointMake(0, 220)animated:YES]; 
    


【讨论】:

每个文本框都需要一个标签来与提到的标签属性相对应,但是这个代码字对 cgpointmake 值进行了一些小的调整,所以它在第 10 个文本字段上向上滚动了正确的数量,这里没有提到,谢谢我的帮助 是的,我的意思是替换上面代码中的值。

以上是关于多个文本字段的滚动视图不起作用的主要内容,如果未能解决你的问题,请参考以下文章

UITextView 在分页 UIScrollView 内滚动不起作用

子视图滚动不起作用 SWIFT

全屏视图在 HStack 和水平滚动视图中不起作用

粘贴长文本时UITextView动态高度滚动不起作用

使用 TPKeyboardAvoidingScrollView 移动键盘的文本字段不起作用

对 SwiftUI 中水平滚动视图内的列表不起作用的操作