滚动后未启用UIScrollView中的UITextFied

时间:2015-09-25 05:06:26

标签: ios uiview uiscrollview uitextfield swift2

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

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

在模拟器/ iPhone中运行App时,屏幕上显示的前6 UITextFields工作[响应触摸]无需滚动,剩余4 UITextFields无法正常工作[不响应触摸],滚动后会显示

请任何人指导我解决此问题吗?

谢谢

更新: 我在ViewController中使用它

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

ViewControllerStructure如下 enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个适用于我的代码

in .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];
}

答案 1 :(得分:0)

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