I'm new to ios app development. In a sample login page there are many view objects in a view.When i try to edit the textfields at the bottom of the screen, the keyboard is appeared and it is over the view object that i need to edit.So, the view object is covered with keyboard itself. How to view all the view objects at the bottom of the screen properly even if keyboard is appeared.? Thanks in Advance.
问问题
51 次
1 回答
0
添加滚动视图,在其上显示您的控件,添加键盘通知,调整滚动内容。
一个很好的文件可以在这里找到
- (void)viewDidLoad
{
@try
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}
- (void)keyboardWasShown:(NSNotification *)notification
{
@try
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSizePotriat = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGSize keyboardSize = {keyboardSizePotriat.height,keyboardSizePotriat.width};
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.theScrollView.contentInset = contentInsets;
self.theScrollView.scrollIndicatorInsets = contentInsets;
CGPoint scrollPoint = CGPointMake(0.0, self.txtMRN.frame.origin.y - (keyboardSize.height - 45));
[self.theScrollView setContentOffset:scrollPoint animated:YES];
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}
- (void) keyboardWillHide:(NSNotification *)notification
{
@try
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.theScrollView.contentInset = contentInsets;
self.theScrollView.scrollIndicatorInsets = contentInsets;
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}
于 2013-03-17T02:21:52.170 回答