請,你能糾正我的代碼,所以我有一個簡單的應用程序與文本字段,當鍵盤結束時向上移動的例子嗎?ios移動滾動視圖時,鍵盤結束文本字段
來實現它,但我不知道他們所說的「有源現場被存儲在一個自定義變量(在這個例子叫activeField)」和我可能還有其他錯誤。在viewWillAppear中使用registerForKeyboardNotifications是好的?
我知道這個問題有一些線索,但我是新手,很難爲我理解它們。我不希望只是學習如何,但爲什麼,這就是爲什麼我不想用準備使用的解決方案從GitHub其他建議等
我的代碼大氣壓:
.H:
#import <UIKit/UIKit.h>
@interface VNViewController : UIViewController<UIScrollViewDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *texticek;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@end
.M:
#import "VNViewController.h"
@interface VNViewController()
@end
@implementation VNViewController
@synthesize scrollView;
@synthesize texticek;
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Call this method somewhere in your view controller setup code.
- (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);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
@end
[鍵盤在活動文本字段上滾動 - 滾動到視圖外?](http://stackoverflow.com/questions/7193787/keyboard-scroll-on-active-text-field-scrolling-to-out-of-view) –
我不明白爲什麼它應該是重複的。 –
請嘗試https://github.com/simonbs/BSKeyboardControls – Vinodh