【问题标题】:UITextFied in UIScrollView not enabled after scrolling滚动后未启用 UIScrollView 中的 UITextFied
【发布时间】:2015-12-22 20:57:01
【问题描述】:

我在 ViewController 中添加了 UIScrollView,并在 UIScrollView 下添加了 UIView

我在那个UIView中添加了总数10个UITextFields, 滚动工作正常

在模拟器/iPhone 中运行 App 时,前 6 个 UITextFields 工作 [Responding to touches] 显示在屏幕上而不滚动,其余 4 个 UITextFields 不工作 [Not response to the touches],这将滚动后显示

谁能指导我解决这个问题?

谢谢

更新: 我在 ViewController 中使用过

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 1070);
    }

ViewControllerStructure 如下

【问题讨论】:

    标签: ios uiview uiscrollview uitextfield swift2


    【解决方案1】:

    试试这个对我有用的代码

    在.h中

    {
        IBOutlet UIScrollView *scrView;
            UITextField *currentTextField;
            BOOL keyboardIsShown;
    }
    -(IBAction)backButtonClicked:(id)sender;
    

    在.m文件中

    //---size of keyboard---
    CGRect keyboardBounds;  
    //---size of application screen---
    CGRect applicationFrame;
    //---original size of ScrollView---
    CGSize scrollViewOriginalSize;
    
    
    - (void)moveScrollView:(UIView *) theView {  
        //---get the y-coord of the view---
        CGFloat viewCenterY = theView.center.y;  
    
        //---calculate how much free space is left---
        CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;      
    
        CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.5;  
    
        if (scrollAmount < 0)  scrollAmount = 0;  
    
        //---set the new scrollView contentSize---
        scrView.contentSize = CGSizeMake(
                                            applicationFrame.size.width, 
                                            applicationFrame.size.height + keyboardBounds.size.height);  
    
        //---scroll the ScrollView---
        [scrView setContentOffset:CGPointMake(0, scrollAmount) animated:YES];  
    }
    
    //---when a TextField view begins editing---
    -  (void)textFieldDidBeginEditing:(UITextField *)textFieldView {  
        [self moveScrollView:textFieldView];
    }  
    
    //---when a TextField view is done editing---
    - (void)textFieldDidEndEditing:(UITextField *) textFieldView {  
        //[scrollView setContentOffset:CGPointMake(0, -(dis * 2.0)) animated:YES];  
        [UIView beginAnimations:@"back to original size" context:nil];
        scrView.contentSize = scrollViewOriginalSize;
        [UIView commitAnimations];
    
    }
    
    //---when the keyboard appears---
    - (void)keyboardWillShow:(NSNotification *) notification
    {
        //---gets the size of the keyboard---
        NSDictionary *userInfo = [notification userInfo];  
        NSValue *keyboardValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];  
        [keyboardValue getValue:&keyboardBounds];   
    }
    
    - (void)keyboardWillHide:(NSNotification *) notification
    {
    
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textFieldView
    {
        [textFieldView resignFirstResponder];
    
        return NO;
    
    }
    
    -(void)viewWillAppear:(BOOL)animated
    {   
        //---registers the notifications for keyboard---
        [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector:@selector(keyboardWillShow:) 
         name:UIKeyboardWillShowNotification 
         object:self.view.window]; 
    
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(keyboardWillHide:)
         name:UIKeyboardWillHideNotification
         object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] 
         removeObserver:self 
         name:UIKeyboardWillShowNotification 
         object:nil];
    
        [[NSNotificationCenter defaultCenter] 
         removeObserver:self 
         name:UIKeyboardWillHideNotification 
         object:nil];
    }
    
    - (void)viewDidLoad 
       {        
            txt1.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt2.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt3.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt4.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt5.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt6.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt7.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt8.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt9.keyboardAppearance = UIKeyboardAppearanceAlert;
            txt10.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    
            scrollViewOriginalSize = scrView.contentSize;
            applicationFrame = [[UIScreen mainScreen] applicationFrame];    
            [super viewDidLoad];
    }
    
    
    - (void)dealloc {
        [scrView release];
        [super dealloc];
    }
    

    【讨论】:

    • 你能解释一下它在做什么吗,我用的是swift,我不明白它到底在做什么
    • 能否将您的代码与您的问题分享一下,这样就很容易理解您在代码中缺少什么
    • 我更新了问题请检查
    【解决方案2】:

    您的文本字段位于超级视图范围之外。您应该调整视图的大小,以便所有文本字段都在范围内。

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多