How to resize iphone uitextfiled keyboard - xcode

Am working in iphone application. In my application i need to resize my uitextfield keyboard.
When i click uitextfield the keyboard appear half of the screen. So i need to reduce height of the keyboard. Can any one help me out of this issue?. Thanks in advance.

You are not allowed to resize the keyboard. Apple does not allow it and doing so will lead to app rejection. You should move the view up for the keyboard not to hide textfields. Add these to your viewcontroller.m file and do not forget to conform to UITextFieldDelegate in your .h file:
#define kOFFSET_FOR_KEYBOARD 80.0 //on top of .m file under imports
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:self.passField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
In this way the view will move up when the keyboard is shown.

Related

Is there a change to the behaviour of UIKeyboardWillShowNotification in iOS 8?

I have had a simple UIView block animation that handles animating a group of text fields into view when the keyboard will show (and animate them back when the keyboard hides). This has worked fine in iOS 6 & 7, but now I'm getting incorrect behavior, and it all points to some change in UIKeyboardWillShowNotification.
I set up an isolated project to test this further on a single text field, with two buttons that call exactly the same methods that are fired for the keyboard's WillShow and WillHide notifications. See the results in this video:
Video example
This seems like a bug to me, or it might be a change to the behavior of this notification. Does anyone know if this is intended and/or what can be done about it?
Here is the relevant code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
self.textField.frame = CGRectOffset(self.textField.frame, 0.f, _destY - _origY);
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
self.textField.frame = CGRectOffset(self.textField.frame, 0.f, _origY - _destY);
}];
}

Content offset issue after watching video in iOS 8

I have a TabBarController, supportedInterfaceOrientations - UIInterfaceOrientationMaskPortrait. Navigation bar moves up under the status bar when I start watching video from one of tabs, rotate device to landscape and then exit from fullscreen. Why it's happens and how fix it?
This is a kind of hack, but works in my view controller contains UIWebView and fullscreen videos start from <video> tags.
You will see jerking of navigation bar because UIWindowDidBecomeHiddenNotification is called after video fullscreen window just disappear.
- (void)viewDidLoad
{
[super viewDidLoad];
// NOTE: I'm not sure, but MPMoviePlayerWillExitFullscreenNotification won't work
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)windowDidBecomeHidden:(NSNotification *)notification
{
UIView *navbar = self.navigationController.navigationBar;
CGRect barFrame = navbar.frame;
barFrame.origin.y = 20;
self.navigationController.navigationBar.frame = barFrame;
UIView *navbarBack = nil;
for (UIView *view in navbar.subviews) {
if ([NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"]) {
navbarBack = view;
break;
}
}
CGRect backFrame = navbarBack.frame;
backFrame.origin.y = -20;
backFrame.size.height = 64;
navbarBack.frame = backFrame;
[navbar.superview setNeedsLayout];
}
UIView *navbarBack = [navbar.subviews bk_match:^BOOL(UIView *view) {
return [NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"];
}];
I have met the same issue.I tried to use js to get the "webkitendfullscreen" event,and then found navbar.frameOriginY = 0,which should be 20.

How to disable MasterView when the keyboard appears in the DetailView

I would like to know if it's possible (and how) when the keyboard appears in the DetailView, to disable any MasterView controls until it disappears. All of this happens in a split view based app of course.
---Update for Prince's solution---
MasterViewController.h
#property (strong, nonatomic) UIView *MasterView;
MasterViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MasterView=self.view;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
DetailViewController.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
((MasterViewController *)self.parentViewController).MasterView.userInteractionEnabled=NO;
return YES;
}
This code as is, crashes the app with an "Unknown Selector" error.
How do i bind delegates; Don't know if that's the problem or not. Any help?
Use UITextField's delegate and also bind delegates:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
MasterView.userInteractionEnabled = NO;
.......
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
MasterView.userInteractionEnabled = YES;
[textField resignFirstResponder];
return YES;
}
I found out a solution!
in MasterView viewDidLoad:
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
and then...:
//----------Handling Keyboard Appearence---
-(void) keyboardDidShow:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:NO];
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:YES];
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

move frame up when keyboard pops in Xcode

#import "LoginScreen.h"
#define kTabBarHeight 1
#define kKeyboardAnimationDuration 0.3
#implementation LoginScreen
#synthesize userName,password,loginButton,scrollView;
BOOL keyboardIsShown;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
//make contentSize bigger than your scrollSize (you will need to figure out for your own use case)
// CGSize scrollContentSize = CGSizeMake(1024,700 );
// [scrollView setContentSize : scrollContentSize];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [boundsValue CGRectValue].size;
// resize the scrollview
CGRect viewFrame = self.scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height += (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
// This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown.
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [boundsValue CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = YES;
}
- (IBAction) loginButton: (id) sender{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end
Can anybody tell me whats wrong in code below. The original view doesn't move up even though i am subtracting the keyboards height to the frame heights.
The scrollView doesn't move up when keyboard pops in? Am I missing some code here.
Well, the problem is that you're not telling your code where your text field is to begin with, so it has no idea where it needs to scroll from. You probably thought that your self.scrollView.frame does it, but that only tells the code the size of your ScrollView, not that it needs to scroll or should scroll. Here's what I did to get mine to work.
Look at the connections for one of your text fields. Drag and drop from the "Did Begin Editing" and "Did End Editing" into your .h file to create IBAction function declarations. Xcode is going to put functions into the .m, but replace them with these:
//even though these functions don't reference the IBAction that we placed for the
"DidBeginEditing" sender for a text field, it will still call these functions.
We need to let the code know what text field we just touched so we can go through
the functions that reset the view size if the text field is under the keyboard.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
currentTextField = nil;
}
That reminds me, you'll need to declare currentTextField as a UITextField * in the .m:
#interface ThirdView : UIViewController{
UITextField *currentTextField;
BOOL keyboardIsShown;
}
My viewDidLoad, keyBoardWasShown, etc are a bit different from yours, but I'll just put them all here so you can see how I got it to work:
//This is code you actually add to get the view to scroll.
You should first connect an outlet from the ScrollView to the .h file so these
functions become available.
- (void)viewDidLoad {
//standard screen size is 320 X 460
// ---set the viewable frame of the scroll view---
// scrollView.frame = CGRectMake(0, 0, 320, 460);
//---set the content size of the scroll view---
// [scrollView setContentSize:CGSizeMake(320, 615)];
//the status bar is 20 pixels tall
//the navigation bar is 44 pixels tall
//---set the viewable frame of the scroll view---
//Note: for some reason, the origin (0,44) doesn't take into account the status bar, but it works anyway. However, the height of the scroll view does take it into account. Wierd, but whatever. So you have to make y1 = 44, and y2 = 460-44-20.
scrollView.frame = CGRectMake(0, 44, 320, 416);
//---set the content size of the scroll view---
[scrollView setContentSize:CGSizeMake(320, 571)];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setPrincipal_Amt:nil];
[self setAPR:nil];
[self setYears:nil];
[self setScrollView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if(keyboardIsShown)
return;
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
Notice that we've got to add 64 pixels to the keyboard height because the CGRect has
no idea that you have the status bar shown and a navigation bar within your view, so
you have to manually add it in.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height + 64);
if (!CGRectContainsPoint(aRect, currentTextField.frame.origin) )
{
CGPoint scrollPoint = CGPointMake(0.0, currentTextField.frame.origin.y-kbSize.height + 64);
[scrollView setContentOffset:scrollPoint animated:YES];
}
keyboardIsShown = YES;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
keyboardIsShown = NO;
}
//
I hope that helps...even though you did post this a while ago :) It took me a while to get it to work too. Very frustrating.

Making the view scrollable when keyboard appears

Managed to get it on my first view, but doesnt work on second view.
Here's what I did on both view, with only a slight differencs for debugging purposes in console
-(void) viewWillAppear:(BOOL)animated {
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
if (keyboardIsShown) return;
NSLog(#"Keyboard is visible 1"); // debugger purpose "Keyboard is visible 2" on the second view.
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view (with keyboard)---
CGRect viewFrame = [scrollview frame];
NSLog(#"%f", viewFrame.size.height);
viewFrame.size.height -= keyboardRect.size.height;
scrollview.frame = viewFrame;
NSLog(#"%f", keyboardRect.size.height);
NSLog(#"%f", viewFrame.size.height);
//---scroll to the current text field---
CGRect textFieldRect = [currentTextField frame];
[scrollview scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size
// (without keyboard)---
CGRect viewFrame = [scrollview frame];
viewFrame.size.height += keyboardRect.size.height;
scrollview.frame = viewFrame;
keyboardIsShown = NO;
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
You are registering for UIKeyboardDidShowNotification and UIKeyboardDidHideNotification notifications, and then deregistering for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications. There's your error.

Resources