Scroll view - How to keep the objects in the view
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 mainScr开发者_运维百科een] 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];
}
You can use the UIControlEventValueChanged event.
[switchControl addTarget:self action:@selector(scrollViewToCenterOfScreen:) forControlEvents:UIControlEventValueChanged];
精彩评论