条件滚动视图

时间:2012-05-26 20:07:14

标签: ios uiscrollview nsnotificationcenter

我有TextField1和TextField2
由于TextField2,我只想在显示键盘时滚动滚动视图。 这是我的实际代码 有没有解决方案?

-(void) viewWillAppear:(BOOL)animated {    
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:self.view.window];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];
}

-(void) keyboardDidShow:(NSNotification *) notification {
   self.ScrollView.center = CGPointMake(self.originalCenter.x,
                                        self.originalCenter.y-100);
}

-(void) keyboardDidHide:(NSNotification *) notification {   
    self.ScrollView.center = CGPointMake(self.originalCenter.x,
                                         self.originalCenter.y);
}

1 个答案:

答案 0 :(得分:1)

您需要侦听UITextfield委托方法:

textfield2.delegate = self;

-(void)textFieldDidBeginEditing: (UITextField*)textField {
    if (textField == textField2) {
        //ENABLE THE SCROLLING
    }
}

-(void)textFieldDidEndEditing: (UITextField*)textField {
    if (textField == textField2) {
        //DISABLE THE SCROLLING
    }
}

只需根据需要自定义方法。

如果需要完全在键盘显示的时候,你可以检查一下bool:

if (textField == textField2) {
    scrollBool = YES;
    }
}

-(void)textFieldDidEndEditing: (UITextField*)textField {
    if (textField == textField2) {
        scrollBool = NO;
    }
}

-(void)keyBoardDidShow.... {
    if (scrollBool) {
        // do the scrolling
    }
}