У меня есть один экран, и на этом экране у меня есть 10 текстовых полей, теперь моя проблема в том, что клавиатура открыта, а затем клавиатура на моем 3 текстовом поле. так как я могу управлять этим с помощью UIScrollView и NSNotification Center?Как управлять ScrollView (вверх и вниз) В соответствии с клавиатурой было показано и скрыто на экране с использованием NSNotificationCenter в Objective C
ответ
Да, у меня есть один код. Для этого я уверен, что определенно работает для вас.
[[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);
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}
, где мне нужно добавить метод addobserver? –
Вам нужно добавить этот метод AddObserver в ViewDidLoad или любой другой метод жизненного цикла ViewController в соответствии с вашими требованиями. метод работы уведомляет NSNotificationCenter for, Keyboard Show And Hide, – sumit
Вы можете создать категорию UIView, чтобы он имеет возможность наблюдать за UIKeyboardWillShowNotification
и UIKeyboardWillHideNotification
Извещение
, как показано ниже
@implementation UIView (PFInputViewControl)
-(void)setupKeyboardShow:(void(^)(NSNotification *notifacation))showblock hide:(void (^)(NSNotification *notifacation))hideblock {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQuene = [NSOperationQueue mainQueue];
[notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:showblock];
[notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:hideblock];
//Setting tap hid gesture when the keyboard is showed and remove the tap gesture when it disappear
[self setupForDismissKeyboard];
}
-(void)setupForDismissKeyboard{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHidAction)];
__weak typeof(self)weakSelf = self;
NSOperationQueue *mainQuene = [NSOperationQueue mainQueue];
[notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification * _Nonnull note) {
[weakSelf addGestureRecognizer:singleTap];
}];
[notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:^(NSNotification * _Nonnull note) {
[weakSelf removeGestureRecognizer:singleTap];
}];
}
@end
И в вашем использовании, вам просто нужно импортировать файл категории, а затем установить код
[self.view setupKeyboardShow:^(NSNotification *notification) {
//========== do your action when keyboard is show ==========
//....
//
//such as
NSDictionary* info = [notification userInfo];
//get the size of key board
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//make transform when key board show
self.view.y = - kbSize.height;
} hide:^(NSNotification *notification) {
//.... key board hid action
self.view.y = 0;
//....
}];
//setup keyboard dismiss when touch the screen out side the keyboard
[self.view setupForDismissKeyboard];
Вы можете использовать этот быстрый кода в Objective C. Просто нужно написать этот код один раз и использовать в любом месте. Напишите этот метод в вашем BaseViewController или MasterViewController, если вы создали. См. :: http://stackoverflow.com/questions/41519044/uiscrollview-stops-scrolling-on-keyboard-dismiss/41519289?noredirect=1#comment70252225_41519289 –