About Hide the keyboard - ios7

My question is :
When I write a user login UI, because the keyboard hide the password textfield, so I write some code to scroll up the login UI, but when the keyboard is hidden and the UI is scrolled to the old position, I find I touch the textfield which I can't get any response.
registe notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillshow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillhide:)
name:UIKeyboardWillHideNotification
object:nil];
_username.delegate = self;
_password.delegate = self;
will show keyboard code:
- (void) keyboardWillshow:(NSNotification *)noti
{
NSDictionary *userInfo = [noti userInfo];
NSValue *keyValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyValue CGRectValue];
int height = keyboardRect.size.height;
userInfo = nil;
keyValue = nil;
[UIView animateWithDuration:0.5f animations:^{
self.containView.frame = CGRectMake(0, -height+20, WIDTH/2, HEIGHT/2-20);
}];
}
will hide keyboard code:
- (void)keyboardWillhide:(NSNotification *)noti
{
[UIView animateWithDuration:0.5f animations:^{
self.containView.frame = CGRectMake(0, 20, WIDTH/2, HEIGHT/2-20);
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_password resignFirstResponder];
[_username resignFirstResponder];
}
Could some one help me? I use the storyboard to write the code.

Related

UIKeyboardWillShowNotification not working on iOS 7

UIKeyboard will show and hide notification method working fine in iOS8 and later, but it not works in iOS7. Is there any alternative?
My Application Deployment target is iOS7.
My Code is here
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(HideKeyboard:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)sender
{
CGSize kbSize =
[[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
if (!scroll) {
scrollValue = self.WholeScreenUIView.frame.origin.y - kbSize.height;
}
scroll = YES;
}
- (void)HideKeyboard:(NSNotification *)sender
{
scroll = NO;
scrollValue = 0.0;
}
Thanks in advance.
Use this code..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHideHandler:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillshowHandler:)
name:UIKeyboardWillShowNotification
object:nil];
- (void) keyboardWillHideHandler:(NSNotification *)notification {
[scroll setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void) keyboardWillshowHandler:(NSNotification *)notification {
[scroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+44)];
}
Hope this helps.This works in my case

Hide iAd Banner SKScene

Hi Guys What Do You Think Should I Use In The Handle Notifications (hideAd,showAd) to show and hide my iAd Banner In My Project and here is what i use to show iAd Banner `#import "GameViewController.h"
import "GameScene.h"
#interface GameViewController(){
bool adOnTop;
bool iadsBannerIsVisible;
ADBannerView* theBanner;
}
#end
#implementation GameViewController
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"showAd" object:nil];
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
skView.multipleTouchEnabled = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFit;
// Present the scene.
[skView presentScene:scene];
[self showThinBanner];
}
-(void) showThinBanner {
iadsBannerIsVisible = YES;
theBanner = [[ADBannerView alloc] initWithFrame:CGRectZero];
[theBanner setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
theBanner.delegate = self;
[self.view addSubview:theBanner];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES; // your app implements this method
if (!willLeave && shouldExecuteAction){
// insert code here to suspend any services that might conflict with the advertisement, for example, you might pause the game with an NSNotification like this...
[[NSNotificationCenter defaultCenter] postNotificationName:#"PauseScene" object:nil]; //optional
}
return shouldExecuteAction;
}
-(void) bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(#"banner is done being fullscreen");
//Unpause the game if you paused it previously.
[[NSNotificationCenter defaultCenter] postNotificationName:#"UnPauseScene" object:nil]; //optional
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (iadsBannerIsVisible == YES) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
iadsBannerIsVisible = NO;
NSLog(#"banner unavailable");
}
}
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:#"hideAd"])
{
// hide your banner;
}else if ([notification.name isEqualToString:#"showAd"])
{
// show your banner
}
}
Sloved
- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:#"hideAd"])
{
// hide your banner;
[theBanner removeFromSuperview];
}else if ([notification.name isEqualToString:#"showAd"])
{
// show your banner
[self.view addSubview:theBanner];
}
}

MPMoviePlayerController does not dismiss view when clicking done?

I use a MPMoviePlayerController to play a video from Internet.
player = [player initWithContentURL:[NSURL URLWithString:videoURL]];
player.view.frame = CGRectMake(0, 0, videoView.frame.size.width, videoView.frame.size.height - 20);
[player setControlStyle:MPMovieControlStyleEmbedded];
player.scalingMode = MPMovieScalingModeAspectFit;
[player prepareToPlay];
player.shouldAutoplay = NO;
[videoView addSubview:player.view];
I notified that after I clicked the full screen button (2-arrows-button), I was navigated to the full size video screen. I couldn't restore down the screen by touching the Done button. I even used NSNotification but can't resolve the problem. Here is the Notification code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieEventFullscreenHandler:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieEventFullscreenHandler:)
name:MPMoviePlayerDidEnterFullscreenNotification
object:nil];
}
- (void)movieEventFullscreenHandler:(NSNotification*)notification {
[player setFullscreen:NO animated:NO];
[player setControlStyle:MPMovieControlStyleEmbedded];
}
How can I dismiss that video screen by touching the Done button? Thanks guys.
You can use the notification of MPMoviePlayerPlaybackDidFinishNotification to observe the done button.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playObserver:)name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
and then,make a judge of quitting.
- (void) playObserver:(NSNotification *)notification
{
MPMoviePlayerController* player = moviePlayerView.moviePlayer;
if (player == [notification object]) {
if (_invalidVideoCount > MOVIE_TRY_TIMES) {
[self dismissViewController];
}
_invalidVideoCount++;
int reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
//Whether continuous playback
if (![SINGLETON_CALL(SystemInfoManager) boolValueForKey:UserContinuousPlayEnableKey]) {
[self playFinishWithForce:YES];
return;
}
switch (reason) {
case MPMovieFinishReasonUserExited:
[self playFinishWithForce:YES];
break;
case MPMovieFinishReasonPlaybackError:
[self playFinishWithForce:YES];
break;
case MPMovieFinishReasonPlaybackEnded:
movieTryTimes = 0;
[self playFinishWithForce:NO];
break;
default:
break;
}
}
}
and the last.
- (void)playFinishWithForce:(BOOL)force
{
FileInfoItem *item = ARRAY_OBJECT_AT_INDEX(_playlist, _currentIndex);
BOOL quit = force || !item;
if (quit) {
[self dismissViewController];
} else {
[self playMovieWithItem:item];
}
}
Also you can use the notification of MPMoviePlayerPlaybackStateDidChangeNotification to make any observe.something detail see MPMoviePlayerController.h or https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html
I have found the problem. That's I laid [player stop] in the viewWillDisAppear so can't handle the notification. I fixed temporary by changing it to [player pause]. I appreciated any kind of your helps.

Keyboard hide UITableView issue

I have a view controller on which I have a table view, in table view each row have text field when i tap on the text field keyboard appear and hide the table view, and then can't see the editing, how can I fix this issue? I put observer that fix the alignment of view controller but it didn't work here is the code..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
return YES;
}
-(void)keyboardWillShow {
// Animate the current view out of the way
[UIView animateWithDuration:0.3f animations:^ {
self.viewFrame = CGRectMake(0, -160, 320, 480);
}];
}
you have to write below code, it will hide and show keyboard.
====>.h file: declare one UITextField as below:
UITextField *actifText;
====>.m file:
-(void)viewDidAppear:(BOOL)animated
{
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;
// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Reduce size of the Table view
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
frame.size.height -= keyboardBounds.size.height;
else
frame.size.height -= keyboardBounds.size.width;
// Apply new size of table view
self.tblName.frame = frame;
// Scroll the table view to see the TextField just above the keyboard
if (self.actifText)
{
CGRect textFieldRect = [self.tblName convertRect:self.actifText.bounds fromView:self.actifText];
[self.tblName scrollRectToVisible:textFieldRect animated:NO];
}
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.actifText = nil;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.actifText = textField;
}
-(void) keyboardWillHide:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.tblName.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Reduce size of the Table view
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
frame.size.height += keyboardBounds.size.height;
else
frame.size.height += keyboardBounds.size.width;
// Apply new size of table view
self.tblName.frame = frame;
[UIView commitAnimations];
}
When you tap on some textfield inside cell, you must know current cell's indexpath, so when keyboard is shown, you can focus it.
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell *)[textField superview].superview;
NSIndexPath *idxPath = [table indexPathForCell:cell];
selectedIndex = idxPath.row;//instance variable
return YES;
}
-(void)keyboardWillShow {
[table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Also add following line to ViewDidLoad, keeping in mind to observe when keyboard have appeared. If you use UIKeyboardWillShowNotification, first keyboardWillShow will be called followed by textFieldShouldBeginEditing: which is wrong.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardDidShowNotification
object:nil];
and set
textfield.delegate = self;
in cellForRowAtIndexPath while creating textfield. It will call required methods.

call method AFTER textFieldShouldReturn

I would like to call my login method AFTER the keyboard was dismissed - since I want to start animation and change the UIView alpha to 0.5 until the response.
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.passwordText){
//hide the keyboard
[theTextField resignFirstResponder];
[self validateCredentialsRemotely];
}else{
[self.passwordText becomeFirstResponder];
}
return YES;
}
The keyboard is not being dismissed before the call to the method validateCredentialsRemotely and the screen freezes while the keyboard is displayed. I would like it to be dismissed first, and then call the method.
Use this notifications..
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
In keyboardWillHide method call your validateCredentialsRemotely method,This may solve your first issue
try this!
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.passwordText){
//hide the keyboard
[theTextField resignFirstResponder];
[self performSelector:#selector(doAnim) withObject:nil afterDelay:0];
}else{
[self.passwordText becomeFirstResponder];
}
return YES;
}
- (void)doAnim {
//start animation
self.view.alpha =0.5;
[activityWheel startAnimating];
//validate user
[self validateCredentialsRemotely];
//end animation
[activityWheel stopAnimating];
self.view.alpha =1;
}