【问题标题】:Scroll view - How to keep the objects in the view滚动视图 - 如何将对象保留在视图中
【发布时间】:2011-09-02 16:02:51
【问题描述】:

我有一个滚动视图,其中有许多 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];  



}  

【问题讨论】:

    标签: iphone uiscrollview uibutton uiswitch


    【解决方案1】:

    您可以使用 UIControlEventValueChanged 事件。

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

    【讨论】:

    • 我是否会添加对所有 UISwitch 对象的引用,然后为每个对象添加一行,就像您的示例一样? Aso ScrollViewtoCenter... 需要将视图发送给它。在你的例子中我会怎么做?
    • 是的,您必须为每个 UISwitch 编写上述代码。当调用方法 scrollViewToCenterOfScreen 时,它将获取触发事件的 View 作为参数,因此您可以直接使用该视图执行任何操作。
    • 事件默认传递发送者对象(它与你的 scrollViewToCenterOfScreen 方法的签名相匹配)。只需为表单中的每个 UISwitch 控件添加上述行,一切都会按原样运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多