滚动视图 - 如何将对象保留在视图中
我有一个滚动视图,其中有许多 textfields
,然后是一些 UISwitches
和 UIButtons
,然后是更多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 UIControlEventValueChanged 事件。
You can use the UIControlEventValueChanged event.