0

我有 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);
}
4

1 回答 1

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
    }
}
于 2012-05-26T21:34:09.277 回答