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

发布于 2024-12-02 10:58:28 字数 1052 浏览 1 评论 0原文

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

我找到了 textFieldDidBeginEditing 事件,并使用该事件将 textfield 滚动到视图中。但我还没有发现 UISwitch 和 UIButton 等其他对象的类似内容。

有什么东西可以做到这一点吗?以下是我用于文本字段的代码:

- (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];  



}  

I have a scroll view which has numerous textfields and then some UISwitches and UIButtons followed by more textfields.

I have found the textFieldDidBeginEditing event and use that to scroll the textfield into the view. But I have not found something similar for the other objects like the UISwitch and UIButton.

Is there something that does this? Below is the code I use for the 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];  



}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-12-09 10:58:28

您可以使用 UIControlEventValueChanged 事件。

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

You can use the UIControlEventValueChanged event.

[switchControl addTarget:self action:@selector(scrollViewToCenterOfScreen:) forControlEvents:UIControlEventValueChanged];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文