我在注册屏幕上有文本字段,所以当键盘出现时我向上移动视图。现在我想在我的视图向上移动时滚动文本字段。
用于移动查看的代码:
CGRect rect = self.view.frame;
rect.origin.y += 220;
rect.size.height -= 220;
self.view.frame = rect;
这是hirecrchy
我在UIScrollView
下面UIScrollView
UIView
我contentView
我称之为contentView
。 self.cvHeight.constant=500;
self.scrollView.contentSize=CGSizeMake(500, 500);
我添加了一些文字字段。所以我的父视图移动得很好,但我无法向上滚动textFields&当我查看时,我失望了。我为滚动设置了以下值。
cvHeight
此处contentView
是scrollView
&的高度约束的出口。 UIScrollView
是self.view
修改
当我没有移动<p:ajax>
的parentView时,我可以滚动浏览,但如果我向上移动父视图,那么我的textFields不向下滚动它们只是向上移动。
答案 0 :(得分:0)
当您使用UIScrollView时,这将是最适合您的。使用TPKeyboardAvoidingScrollView
如何使用? 它非常简单
要与UITableViewController类一起使用,请将TPKeyboardAvoidingTableView.m和TPKeyboardAvoidingTableView.h拖放到项目中,并使您的UITableView成为xib中的TPKeyboardAvoidingTableView。如果您没有在控制器中使用xib,我知道没有简单的方法可以使其UITableView成为自定义类:阻力最小的路径是为它创建一个xib。
对于非UITableViewControllers,将TPKeyboardAvoidingScrollView.m和TPKeyboardAvoidingScrollView.h源文件放入项目中,将UIScrollView弹出到视图控制器的xib中,将滚动视图的类设置为TPKeyboardAvoidingScrollView,并将所有控件放在该滚动视图中。您也可以在不使用xib的情况下以编程方式创建它 - 只需使用TPKeyboardAvoidingScrollView作为顶级视图。
答案 1 :(得分:0)
<强> ViewController.h 强>
#import <UIKit/UIKit.h>
@interface SignUp : UIViewController <UIScrollViewDelegate>
{
}
@property (nonatomic, assign) UITextField *activeTextField;
<强> ViewController.m 强>
@implementation SignUp
@synthesize activeTextField;
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#pragma mark - Textfield
- (void)keyboardWasShown:(NSNotification *)notification
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// 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);
_scrl.contentInset = contentInsets;
_scrl.scrollIndicatorInsets = contentInsets;
// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) )
{
CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height));
[_scrl setContentOffset:scrollPoint animated:YES];
}
}
- (void) keyboardWillHide:(NSNotification *)notification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrl.contentInset = contentInsets;
_scrl.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField = nil;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
{
if(textField == _txt_name)
{
[_txt_email becomeFirstResponder];
return NO;
}
else if(textField == _txt_email)
{
[_txt_pwd becomeFirstResponder];
return NO;
}
else if(textField == _txt_pwd)
{
[_txt_repwd becomeFirstResponder];
return YES;
}
else if (textField == _txt_repwd)
{
[textField resignFirstResponder];
return YES;
}
else
{
return YES;
}
}
答案 2 :(得分:0)
- (void)keyboardWillShow:(NSNotification*)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0,0.0,keyboardSize.height, 0.0);
self.scrollViewPost.contentInset = contentInsets;
self.scrollViewPost.scrollIndicatorInsets = contentInsets;
CGRect rect = self.scrollViewPost.frame;
rect.size.height = keyboardSize.height+_scrollViewPost.frame.size.height+84;
if (!CGRectContainsPoint(rect, self.txtPost.frame.origin))
{
CGPoint scrollPoint = CGPointMake(0.0, 264+Table_Y);
[self.scrollViewPost setContentOffset:scrollPoint animated:NO];
self.viewPutPost.frame=CGRectMake(0, _viewPutPost.frame.origin.y-254, 320, 30);
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollViewPost.contentInset = contentInsets;
self.scrollViewPost.scrollIndicatorInsets = contentInsets;
self.viewPutPost.frame=CGRectMake(0,537 , 320, 30);
}
答案 3 :(得分:0)
您不必移动整个视图。
相反,根据需要更改scrollview的contentOffset属性,并在键盘启动时相应地增加scrollView.contentSize。 当键盘关闭时,将scrollView.contentOffset设置为CGPointZero和scrollView.contentSize
键盘启动时:
CGFloat yOffset = ; //Set According to the textField
self.scrollView.contentOffset = CGPointMake(0,-yOffset);
//Increase content size to enable scrolling till the end.
CGFloat contentHeight = self.crollView.contentSize.height + keyboardHeight;
self.scrollView.contentSize = CGSizeMake(kScreenWidth, contentHeight);
键盘关闭时:
self.scrollView.contentOffset = CGPointMake(0,0);
//Increase content size to enable scrolling till the end.
CGFloat contentHeight = self.crollView.contentSize.height - keyboardHeight;
self.crollView.contentSize = CGSizeMake(kScreenWidth, contentHeight);
增加内容大小可让您在键盘启动时将滚动视图滚动到底部。