How to move iPhone keyboard down like in Messages.app? [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iMessage Style Receding Keyboard in an iOS App
In the iOS5 Messages app, you can slide your finger down on the keyboard to bring it up and down. This is also done in the iA Writer app in the AppStore. How can I do this in code, that is, access and modify the Y position of the UIKeyboard?

Why reinvent the wheel? There are several open-source projects available that mimick the messages.app receding keyboard:
http://cocoacontrols.com/platforms/ios/controls/madismissivetextview
http://cocoacontrols.com/platforms/ios/controls/dakeyboardcontrol
http://cocoacontrols.com/platforms/ios/controls/imessagekeyboardeffect
To name a few.

There is no method to do this, but you may be able to modify the keyboard's frame directly like this:
UIWindow* tempWindow;
//Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
//UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
//Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
//Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
//Get a reference of the current view
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:#"(lessThen)UIKeyboard"] == YES)
{
//If we get to this point, then our UIView "keyboard" is referencing our keyboard.
}
}
}

Related

How do I place a subview in front of the keyboard?

I hope to insert subview in front of displayed keyboard. I am using the following code:
[self.view bringSubviewToFront: myView];
but the subview does not display.
I am not exactly sure what you are looking for, but my best guess is you want to subview a "done"/"return" over the keypad.
You maybe able to do this by doing something like this (when the keyboard comes up)
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"UIKeyboard"] == YES)
[keyboard addSubview:doneButton];
}
The bringSubviewToFront idea failed because it (the keyboard) is not a subview of your application.
Credit to Artyom from this question
Rather than using this for loop to find the correct window you can instead use:
UIWindow * window = [UIApplication sharedApplication].windows.lastObject;
[window addSubview:_menuView];
[window bringSubviewToFront:_menuView];
As long as you are adding it while the is keyboard active then the keyboard will always be the last view added and it reduces the code complexity greatly.
#Altaf, the prefix you mention in your code is not the good one. You should use:
if([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES)
See an example, with Touchpose classes to show touches on demo applications, that I modified to display the animation over the keyboard.

Hiding the iPhone Keyboard in iOS 5

How do I find and remove the view corresponding to the built-in iPhone keyboard?
In this project, the user enters input into a UITextField that's always the first responder.
In previous versions of iOS (2.1 --> 4, I believe) we added a listener for the
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(keyboardWillShow:)
name: UIKeyboardWillShowNotification
object: nil
];
When the keyboard is about to show, we then remove it (and our textfield remains first responder).
- (void)keyboardWillShow:(NSNotification *)note {
UIWindow* tempWindow;
UIView* keyboard;
UIView* minusView = nil;
for(int c = 0; c < [[[UIApplication sharedApplication]
windows] count]; c ++) {
tempWindow = [[[UIApplication sharedApplication]
windows] objectAtIndex:c];
// Loop through all views in the current window
for(int i = 0; i < [tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
//the keyboard view description always starts with <UIKeyboard
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
minusView = keyboard;
}
}
}
[minusView removeFromSuperview];
}
This approach no longer works, because in iOS 5 there isn't a view of type UIKeyboard that can be found this way.
However, there is a view with the following description <UITextEffectsWindow: 0x693cca0; frame = (0 0; 320 480); hidden = YES; opaque = NO; layer = <UIWindowLayer: 0x693cdb0>> which I have tried to remove with the removeFromSuperview and setIsHidden methods.
Any tips on removing the keyboard and keeping first responder status?
The left screenshot below is the loading screen, which depicts the buttons layout as intended. The right screencap shows how the built-in keyboard obscures the calculator buttons.
You should not be removing the keyboard. The keyboard window is private and, as you have discovered, likely to change between releases.
The correct way to get the effect you want is to set your text field's inputView property to the view that contains your calculator buttons. Then iOS will take care of showing that view for you, instead of showing the default keyboard.
This is documented in “Custom Views for Data Input” in the Text, Web, and Editing Programming Guide for iOS.

iPhone: Is it possible to poll whether the camera is active or not?

We're creating a photo app that lets the user take a number of photos in series. For some reason the camera seems to die with no obvious trigger. This typically appears to happen if the camera is running and the device is idle (screensaver/locking) or if the main iPhone button is pressed and the app is minimized. So we need to find a way to check if the camera is still running or not. Can this be polled somehow? Have someone experienced a similar issue?
If you can get a camera view on self.view you can say that camera is active or present.. here is how you can check if camera view is available or not -
UIView *cameraView = [self findCamControlsLayerView:self.view];
if (cameraView)
// camera is present
else
// camera is not present
// Find the view that contains the camera controls (buttons)
- (UIView*)findCamControlsLayerView:(UIView*)view {
Class cl = [view class];
NSString *desc = [cl description];
if ([desc compare:#"PLCropOverlay"] == NSOrderedSame)
return view;
for (NSUInteger i = 0; i < [view.subviews count]; i++)
{
UIView *subView = [view.subviews objectAtIndex:i];
subView = [self findCamControlsLayerView:subView];
if (subView)
return subView;
}
return nil;
}

Detect when GameCenter UI is displayed

I'm trying to integrate my game with Game Center and encountered this problem:
When user is authenticated for a first time, Game Center shows its UI for setting up the profile.
My problem is that I can not detect when this windows is shown - I want to pause my game at that moment and not play any sounds.
viewWillDisapper, viewDidDisapper in UIViewController are not called, neither are any of AppDelegate methods are called at this time.
I think I know how detect alert views (using changing key window notification), but that Account windows still is not detected there.
Is there any way to do this?
Building on executor21's answer here, I put this together which seems to do the trick in early testing. You can probably adapt it into something less fragile. It is built on the premise that the Game Center notification gets its own window, and it has exactly one subview of type GKGameEventView:
+(BOOL)isGameCenterNotificationUp
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *win in windows)
{
NSArray *winSubViews = [win subviews];
if([winSubViews count] == 1)
{
Class gcNotificationClass = NSClassFromString(#"GKGameEventView");
if(gcNotificationClass && ([[winSubViews objectAtIndex:0] isKindOfClass:gcNotificationClass]))
{
return YES;
}
}
}
return NO;
}

Creating a custom UIKeyBoard for iPhone

If anyone has the app GymBuddy, then they will know what I am talking about. They seem to use the stock Number Pad keyboard but have added a "." button in the lower left as well as a bar across the top to switch to alpha characters. Does anyone know how to do this? Do I make a new view like the keyboard and pull it up and have the buttons correspond to the textField for input? I can't seem to find any information on customizing a keyboard or creating your own. Thanks
I have done this. Basically you add your own button as a subview of the UIKeyboard like this:
// This function is called each time the keyboard is going to be shown
- (void)keyboardWillShow:(NSNotification *)note {
// Just used to reference windows of our application while we iterate though them
UIWindow* tempWindow;
// Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
// UIKeyboard is a subclass of UIView anyways
UIView* keyboard;
// Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
// Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
// Loop through all views in the current window
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
// Get a reference to the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
// From all the apps i have made, they keyboard view description always starts with <UIKeyboard so I did the following
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
// Only add the Decimal Button if the Keyboard showing is a number pad. (Set Manually through a BOOL)
if (numberPadShowing && [keyboard viewWithTag:123] == nil) {
// Set the Button Type.
dot = [UIButton buttonWithType:UIButtonTypeCustom];
// Position the button - I found these numbers align fine (0, 0 = top left of keyboard)
dot.frame = CGRectMake(0, 163, 106, 53);
dot.tag = 123;
// Add images to our button so that it looks just like a native UI Element.
[dot setImage:[UIImage imageNamed:#"dotNormal.png"] forState:UIControlStateNormal];
[dot setImage:[UIImage imageNamed:#"dotHighlighted.png"] forState:UIControlStateHighlighted];
//Add the button to the keyboard
[keyboard addSubview:dot];
// When the decimal button is pressed, we send a message to ourself (the AppDelegate) which will then post a notification that will then append a decimal in the UITextField in the Appropriate View Controller.
[dot addTarget:self action:#selector(sendDecimal:) forControlEvents:UIControlEventTouchUpInside];
return;
}
else if (numberPadShowing && [keyboard viewWithTag:123])
{
[keyboard bringSubviewToFront:dot];
}
else if (!numberPadShowing)
{
for (UIView *v in [keyboard subviews]){
if ([v tag]==123)
[v removeFromSuperview];
}
}
}
}
}
}
- (void)sendDecimal:(id)sender {
// The decimal was pressed
}
Hope that's clear.
-Oscar
Check this post, this could be your answer:
UIKeyboardTypeNumberPad and the missing "return" key