滚动视图 - 如何在视图中保留对象

时间:2011-09-02 16:02:51

标签: iphone uiscrollview uibutton uiswitch

我有一个滚动视图,其中包含许多textfields,然后是一些UISwitchesUIButtons,后跟更多textfields

我找到了textFieldDidBeginEditing事件并使用该事件将textfield滚动到视图中。但我没有找到与UISwitchUIButton等其他对象相似的内容。

有什么能做到的吗?以下是我用于textfields的代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField {  
    [self scrollViewToCenterOfScreen:textField];  
} 



- (void)scrollViewToCenterOfScreen:(UIView *)theView {  
    CGFloat viewCenterY = theView.center.y;  
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  

    CGFloat availableHeight = applicationFrame.size.height - _keyboardHeight;    // Remove area covered by keyboard  

    CGFloat y = viewCenterY - availableHeight / 2.0;  
    if (y < 0) {  
        y = 0;  
    }  
    ScrollView.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + _keyboardHeight);  
    [ScrollView setContentOffset:CGPointMake(0, y) animated:YES];  



}  

1 个答案:

答案 0 :(得分:0)

您可以使用 UIControlEventValueChanged 事件。

[switchControl addTarget:self action:@selector(scrollViewToCenterOfScreen:) forControlEvents:UIControlEventValueChanged];