UITextFied in UIScrollView not enabled after scrol

2019-09-09 00:00发布

问题:

I Added a UIScrollView in a ViewController, And Added UIView under that UIScrollView

I added total number 10 UITextFields in that UIView, Scrolling is Working fine

While running App in Simulator/ iPhone, the First 6 UITextFields Working [Responding to touches] which are shown on the screen without scrolling, Remaining 4 UITextFields are not working [Not responding to the touches], which will shown after scrolling

Please can any one guide me to resolve this issue?

thank you

UPDATE: I Used it in ViewController

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

ViewControllerStructure is as below

回答1:

Try this code it works for me

in .h

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

and in .m file

//---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];
}


回答2:

Your text fields lies outside the superview bounds. You should resize the view so all the text fields will be within bounds.