Iphone 移动位于键盘下的UITextField

Iphone 移动位于键盘下的UITextField,iphone,ios,ios6,uitextfield,uikeyboard,Iphone,Ios,Ios6,Uitextfield,Uikeyboard,所以,就像我确信我遇到的每个iOS程序员一样,当我试图让一个页面充满UITextFields时,很明显,当键盘打开时,它覆盖了其中的一半。所以我去了苹果的文档,阅读了所有关于它的信息,并为它编写了代码,但它对我来说不起作用。每次我运行视图时,它都会崩溃。提前感谢你的帮助 .h文件 @interface NewUserViewController : UIViewController{ } - (IBAction)signMeUpButtonPressed:(id)sende

所以,就像我确信我遇到的每个iOS程序员一样,当我试图让一个页面充满UITextFields时,很明显,当键盘打开时,它覆盖了其中的一半。所以我去了苹果的文档,阅读了所有关于它的信息,并为它编写了代码,但它对我来说不起作用。每次我运行视图时,它都会崩溃。提前感谢你的帮助

.h
文件

 @interface NewUserViewController : UIViewController{

   } 

    - (IBAction)signMeUpButtonPressed:(id)sender;

    @property (weak, nonatomic) IBOutlet UITextField *activeField;
    @property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
    @property (weak, nonatomic) IBOutlet UIButton *backButton;
    @property (weak, nonatomic) IBOutlet UILabel *firstNameLabel;
    @property (weak, nonatomic) IBOutlet UITextField *firstNameInput;
    @property (weak, nonatomic) IBOutlet UILabel *lastNameLabel;
    @property (weak, nonatomic) IBOutlet UITextField *lastNameInput;
    @property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
    @property (weak, nonatomic) IBOutlet UITextField *usernameInput;
    @property (weak, nonatomic) IBOutlet UILabel *emailLabel;
    @property (weak, nonatomic) IBOutlet UITextField *emailInput;
    @property (weak, nonatomic) IBOutlet UILabel *passwordLabel;
    @property (weak, nonatomic) IBOutlet UITextField *setPasswordInput;
    @property (weak, nonatomic) IBOutlet UILabel *reenterPasswordLabel;
    @property (weak, nonatomic) IBOutlet UITextField *checkSetPasswordInput;
    @property (weak, nonatomic) IBOutlet UIButton *signMeUpButton;
所有的都连接到他们的插座上(除了有源场。我不知道它属于哪里或者应该连接到什么)

.m
文件

@implementation NewUserViewController

@synthesize contentScrollView;
@synthesize activeField;
@synthesize firstNameLabel, lastNameLabel, usernameLabel, emailLabel, passwordLabel, reenterPasswordLabel;
@synthesize firstNameInput, lastNameInput, usernameInput, emailInput, setPasswordInput, checkSetPasswordInput;
@synthesize backButton, signMeUpButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    contentScrollView.contentInset = contentInsets;
    contentScrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [contentScrollView setContentOffset:scrollPoint animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    contentScrollView.contentInset = contentInsets;
    contentScrollView.scrollIndicatorInsets = contentInsets;
}

- (IBAction)signMeUpButtonPressed:(id)sender {
} 
试试这个:

- (void)keyboardWasShown:(NSNotification *)notification {



    NSDictionary* userInfo = [notification userInfo];

    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
//offset is global
    offset = contentScrollView.contentOffset;

    CGRect viewFrame = contentScrollView.frame;
    CGRect keyboardFrame = [self convertRect:keyboardEndFrame toView:nil];
    viewFrame.size.height -= keyboardFrame.size.height;
    contentScrollView.frame = viewFrame;

    UITextField *current = (UITextField *)[self findFirstResponder];
    CGRect textFieldRect = current.frame;

    [contentScrollView scrollRectToVisible:textFieldRect animated:YES];

}
- (UIView *)findFirstResponder {
    for (UIView *subView in  scrollView.subviews) {
        if ([subView isFirstResponder]){
            return subView;
        }
    }
    return nil;
}

- (void) keyboardWillBeHidden:(NSNotification *)notification {

//scrollViewFrame is global the first frame of you scrollview
    contentScrollView.frame = scrollViewFrame;

    contentScrollView.contentOffset =offset;


}

这不是一个完整的代码,这只是执行您想要执行的操作的一个小逻辑。

如果发生崩溃,请确保发布崩溃的相关输出,以帮助我们找出错误所在。在没有更多信息的情况下,很难发现崩溃发生的位置:)你需要在滚动面板中添加控件,并在键盘出现时使用滚动来显示控件。***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合key firstNameLable的键值编码。“我不确定amrit_neoI是否能够修复崩溃,但它仍然无法滚动。”