How can the subView detect that the mainView is rotating? - iphone

I have a mainView. To this view, I am adding a view of the same size. When the mainView(the background) rotates, its being detected but the subview doesnt have any idea about being rotated. And its functions are not even being called. Even when the program launches too, if I am in landscape mode, its the same way.
How can I make the subView know that the device is being rotated?

Perhaps you can shoot an event from the mainView to the subView, like so (in mainView):
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
[subView didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}

I quickly grew frustrated by the lack of rotation notification support for non-primary UIViewController instances.
So I baked my own as a UIViewController extension. Note that this is purely for rotation detection within the subview, it won't rotate the subview - I'm working on that part now.
Source code then example usage below.
// Released under license GPLv3.
// Copyright (c) 2012 N David Brown. All Rights Reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// Note: 'shouldAutorotateToInterfaceOrientation:' is automatically called by
// 'willRotate..', 'didRotate..' method calling notification handler
// blocks, so typically will not be desired for notification.
#define NOTIFY_SHOULD_AUTOROTATE 0
#interface UIViewController (NDBExtensions)
// For dispatchers.
#if NOTIFY_SHOULD_AUTOROTATE
-(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation;
#endif
-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation;
// For listeners.
#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate;
#endif
-(void)listenForWillRotate;
-(void)listenForDidRotate;
-(void)listenForAnyRotate;
-(void)stopListeningForAnyRotate;
#end
#implementation UIViewController (NDBExtensions)
#if NOTIFY_SHOULD_AUTOROTATE
-(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation {
NSString *name = #"shouldAutorotate";
NSString *key = #"toInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
#endif
-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSString *name = #"willRotate";
NSString *key = #"toInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
NSString *key2 = #"duration";
NSNumber *val2 = [NSNumber numberWithDouble:duration];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:val,key,val2,key2,nil];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation {
NSString *name = #"didRotate";
NSString *key = #"fromInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:fromInterfaceOrientation];
NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"shouldAutorotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"toInterfaceOrientation"];
UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
[self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}];
}
#endif
-(void)listenForWillRotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"willRotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"toInterfaceOrientation"];
UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
NSNumber *val2 = [[notification userInfo] objectForKey:#"duration"];
NSTimeInterval duration = [val2 doubleValue];
if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
[self willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}];
}
-(void)listenForDidRotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"didRotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"fromInterfaceOrientation"];
UIInterfaceOrientation fromInterfaceOrientation
= (UIInterfaceOrientation)[val intValue];
UIInterfaceOrientation toInterfaceOrientation
= (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
[self didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
}];
}
-(void)listenForAnyRotate {
#if NOTIFY_SHOULD_AUTOROTATE
[self listenForShouldAutorotate];
#endif
[self listenForWillRotate];
[self listenForDidRotate];
}
-(void)stopListeningForAnyRotate {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"shouldAutorotate" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"willRotate" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"didRotate" object:nil];
}
#end
Example usage:
// In PrimaryViewController.h (instance of this contains 'view'
// which is first subview in window).
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
// Normal rules go here.
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// Normal rules go here.
// ..and notification dispatch:
[self notifyWillRotate:toInterfaceOrientation duration:duration];
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// Normal rules go here.
// ..and notification dispatch:
[self notifyDidRotate:fromInterfaceOrientation];
}
// In OtherViewController.h (this could be any non-primary view controller).
-(void)viewDidLoad {
[self listenForAnyRotate];
}
-(void)viewDidUnload {
[self stopListeningForAnyRotate];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
// Normal rules go here.
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// Normal rules go here.
NSLog(#"#willRotate received!");
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// Normal rules go here.
NSLog(#"#didRotate received!");
}

You could fire an NSNotification when the main view is rotated, which the subview is registered to listen for. There's a quick overview of NSNotification over here.
One advantage of this approach is that objects other than subclasses of UIView can listen for this notification.

Related

UIMoviePlayerControllerWillExitFullscreenNotification not received

My app is portrait only but, I would like to allow the user to rotate to landscape when watching full screen videos through a UIWebview. I've done some research and found that I should add my class as an observer for these notifications:
UIMoviePlayerControllerDidEnterFullscreenNotification
UIMoviePlayerControllerWillExitFullscreenNotification
I add and remove the class as an observer like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerDidEnterFullScreen:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillExitFullScreen:) name:#"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
}
- (void)moviePlayerDidEnterFullScreen:(NSNotification *)notification
{
self.videoPlayingFullScreen = YES;
}
- (void)moviePlayerWillExitFullScreen:(NSNotification *)notification
{
self.videoPlayingFullScreen = NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.videoPlayingFullScreen)
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
My problem is: I never receive the "UIMoviePlayerControllerWillExitFullscreenNotification". I can't use the UIMoviePlayerControllerDidExitFullscreenNotification because if the user is finished watching the fullscreen video in landscape orientation and presses "done" the previous view controller also appears in landscape orientation when it should be in portrait.
Is there another way to detect when the user "did" enter fullscreen and "will" exit fullscreen? Or is there something that I am missing?
EDIT:
My app is for iOS 7 only.
The reason you're not getting the UIMoviePlayerControllerWillExitFullscreenNotification callback is because you're removing yourself as an observer on viewWillDisappear:
Since these callbacks are undocumented I used Javascript events (as H2CO3 suggested here) to determine when the video began, ended, or paused.
By the way I'm using the YouTube Player.
First, I setup the UIWebview and set my ViewController as the delegate.
Next, I loaded the HTML file into the UIWebview.
Index.html
<html>
<body bgcolor=#8C1717 style="margin:0px;">
<div id="ytplayer"></div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady()
{
player = new YT.Player('ytplayer',
{
height: 'videoHeight',
width: 'videoWidth',
videoId: 'videoID',
playerVars: { 'showinfo':0, 'rel':0 },
events: { 'onStateChange': onPlayerStateChange }
});
}
function playerDidBeginPlaying()
{
document.location.href = "fake://video-began";
}
function playerDidEndPlaying()
{
document.location.href = "fake://video-ended";
}
var done = false;
function onPlayerStateChange(event)
{
if (event.data == YT.PlayerState.PLAYING && !done)
{
done = true;
playerDidBeginPlaying();
}
else if (event.data == YT.PlayerState.ENDED)
{
playerDidEndPlaying();
}
else if (event.data == YT.PlayerState.PAUSED)
{
playerDidEndPlaying();
}
}
</script>
</body>
</html>
Inside the ViewController
NSError *error = NULL;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error)
{
#ifdef DEBUG
NSLog(#"[YouTube Webview] Error: %#", [error description]);
#endif
}
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Then, I implemented the method webView:shouldStartLoadWithRequest:navigationType: to get notified when the events happened.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:#"fake://video-began"])
{
self.videoPlayingFullScreen = YES;
return NO;
}
else if ([[[request URL] absoluteString] hasPrefix:#"fake://video-ended"])
{
self.videoPlayingFullScreen = NO;
return NO;
}
return YES;
}

How to start and stop CMMotionManager startAccelerating and stopAccelerating while iphone in accelerate mode or in idle mode?

I am developing app related to accelerating. I'm using CMMtionManager object. but when I call startaccelerating method its working and continuously calling that method even iphone in idle mode. How to stop and start accelerating while iphone accelerating and idle.
- (void)viewDidLoad
{
[super viewDidLoad];
motionManager=[[CMMotionManager alloc]init];
motionManager.accelerometerUpdateInterval=2;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startMyMotionDetect];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[motionManager stopAccelerometerUpdates];
// Request to stop receiving accelerometer events and turn off accelerometer
}
- (CMMotionManager *)motionManager
{
motionManager = nil;
id appDelegate = [UIApplication sharedApplication].delegate;
if ([appDelegate respondsToSelector:#selector(motionManager)]) {
motionManager = [appDelegate motionManager];
}
return motionManager;
}
- (void)startMyMotionDetect
{ NSLog(#"active %d",motionManager.accelerometerActive);
NSLog(#"availabilty %d",motionManager.accelerometerAvailable);
[motionManager
startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(#"hello");
[motionManager stopAccelerometerUpdates];
});
}];
}
please assist me.
did you try to add this?:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(stopAccelerometer)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

Keyboard and getting up state on iPhone

How do I find out if the keyboard is up?
I have a UISearchbar instance which becomes the first responder.
When the keyboard appears a notification is sent out as part of the API, however I don't want to respond to this right away. I could record this in a boolean state, but that seems clunky. I'd like to know if there is a "getter" some where I can call to find out.
This is how I do it:
KeyboardStateListener.h
#interface KeyboardStateListener : NSObject {
BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
#property (nonatomic, readonly, getter=isVisible) BOOL visible;
#end
KeyboardStateListener.m
#import "KeyboardStateListener.h"
static KeyboardStateListener *sharedObj;
#implementation KeyboardStateListener
+ (KeyboardStateListener *)sharedInstance
{
return sharedObj;
}
+ (void)load
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sharedObj = [[self alloc] init];
[pool release];
}
- (BOOL)isVisible
{
return _isVisible;
}
- (void)didShow
{
_isVisible = YES;
}
- (void)didHide
{
_isVisible = NO;
}
- (id)init
{
if ((self = [super init])) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(didShow) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:#selector(didHide) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#end
Then use this to figure out the rest:
KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
//Keyboard is up
}
The only sure way that I can think to do it as you said. using notifications like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
and then
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Other than that, you may be able to iterate through your views subviews and look for the keyboard like:
UIView *keyboard = nil;
for (UIView *potentialKeyboard in [myWindow subviews]) {
// iOS 4
if ([[potentialKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0];
}
if ([[potentialKeyboard description] hasPrefix:#"<UIKeyboard"]) {
keyboard = potentialKeyboard;
break;
}
}
But I am not sure if this will break when the SDK changes ...
Maybe use this method and add a category to the window so that you can just always ask the window for the keyboard ... just a thought.

MPMoviePlayerViewController and pinch out for full screen

i have search on the site but i haven't find the same problem as mine
when i do a pinch out on my video, the notification "MPMoviePlayerPlaybackDidFinishNotification" is called.
after, the "done" button put the video in pause and the player works badly...
I don't understand why this notification is called...
this is my code
- (id) init
{
self = [super init];
movie=[[MPMoviePlayerViewController alloc] init];
//we init the frame here and after the view rotate the video
[movie.view setFrame: CGRectMake(0, 0, 1024,768)];
return self;
}
+ (MoviePlayerManager*) getInstance
{
static MoviePlayerManager *movieSingleton;
if (movieSingleton==nil)
{
movieSingleton = [[MoviePlayerManager alloc]init];
}
return movieSingleton;
}
- (void) load:(NSURL*) a_videoFile withType:(VideoType)a_type
{
type = a_type;
[movie.moviePlayer setContentURL:a_videoFile];
switch (type) {
case VT_INTRO:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallbackIntro:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
break;
case VT_RESPONSE:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallbackResponse:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
break;
default:
NSLog(#"video Type not initialised");
break;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieIsReadyToPlay:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:movie.moviePlayer];
[movie.moviePlayer prepareToPlay];
}
-(void)myMovieIsReadyToPlay:(NSNotification*)aNotification
{
[gsDelegate.view addSubview:movie.view];
[movie.moviePlayer play];
movie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}
- (void) myMovieFinishedCallbackIntro:(NSNotification*)aNotification
{
NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
NSLog(#"%d",reason);
if(aNotification != nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
[gsDelegate movieIntroDidStop];
}
}
the NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
is the same for a pinch out or when i press "done"
thx for your help (and sorry for my bad english ;op)
NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
NSLog(#"%d",reason);
NSNumber is an Objective-C object, not a primitive C type. You are displaying the pointer to the object, not the value.
Correct with:
NSLog(#"%#", reason);
OR change reason to an Integer:
int reason = [[userInfo objectForKey:#"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] intValue];
NSLog(#"%d", reason);

UIWebView Keyboard - Getting rid of the "Previous/Next/Done" bar

I want to get rid of the bar on top of the keyboard that appears when you focus a text field in a webview. We have some other ways of handling this and it's redundant and unnecessary.
webview keyboard bar http://beautifulpixel.com/assets/iPhone_Simulator-20100120-152330.png
If you hit this problem, make sure to head over to https://bugreport.apple.com and duplicate rdar://9844216
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[self performSelector:#selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[possibleFormView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
}
}
}
}
This works well.
url: http://ios-blog.co.uk/iphone-development-tutorials/rich-text-editor-inserting-images-part-6/
This is an addition to Yun's answer. On iOS6 (6.0.1) there might be a horizontal grey border or shadow line on top of the row where the accessory (previous / next / done) used to be before it was removed. This fix works for me, and I'd like to share. Curious to hear if it works for you as well.
To remove the border, I added this code to the inner loop of removeBar():
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIImageView"].location != NSNotFound) {
[[subviewWhichIsPossibleFormView layer] setOpacity: 0.0];
}
We need to add the QuartzCore framework to the head of the .m file, so we can set the opacity of the layer involved.
So, we get:
...
#import <QuartzCore/QuartzCore.h>
...
- (void)removeBar {
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
for (UIView *possibleFormView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[possibleFormView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subviewWhichIsPossibleFormView in [possibleFormView subviews]) {
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
[subviewWhichIsPossibleFormView removeFromSuperview];
}
// iOS 6 leaves a grey border / shadow above the hidden accessory row
if ([[subviewWhichIsPossibleFormView description] rangeOfString:#"UIImageView"].location != NSNotFound) {
// we need to add the QuartzCore framework for the next line
[[subviewWhichIsPossibleFormView layer] setOpacity: 0.0];
}
}
}
}
}
It looks like there is a very simple way, but I'm pretty sure it will not pass the App Store review. Maybe someone has a clever idea? ;)
#interface UIWebBrowserView : UIView
#end
#interface UIWebBrowserView (UIWebBrowserView_Additions)
#end
#implementation UIWebBrowserView (UIWebBrowserView_Additions)
- (id)inputAccessoryView {
return nil;
}
#end
There are no public APIs for doing this. You could remove it by examining the view hierarchy and removing the view as some have suggested, but this would be very risky.
Here's why it's a bad idea:
If Apple doesn't have an official API for removing the bar, they may have good reasons for doing so, and their own code may rely on it being there. You might not ever encounter a problem because you do all your testing (for example) on an English keyboard. But what if the view you are removing is required for entry in another language, or for accessibility purposes? Or what if in a future version of iOS their own implementation changes such that it assumes the view is always there? Your code will crash, and you'll be stuck scrambling to get an update out while frustrated users wait for weeks.
Interestingly, Remco's appended answer proves this point. On iOS 6.0.1, a change was made that required a fix to the hack. Anyone who had implemented the hack for ios 5 would have been forced to do an update as a result. Fortunately it was only an aesthetic change, but it could have been much worse.
I was thinking of intercepting the UIKeyboardWillAppear notification, and giving it to a hidden text field instead, and forwarding the events through javascript to the real one in the webview. But it seems hairy. Things cursor movement and selection would then suck.
check out this one. https://gist.github.com/2048571.
It works in iOS 5 and later, doesnt work for earlier versions.
this code definetly works for me... hope this also works for you.
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[self performSelector:#selector(removeBar) withObject:nil afterDelay:0];
}
- (void)removeBar {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIWebFormView
for (UIView *possibleFormView in [keyboardWindow subviews]) {
if ([[possibleFormView description] hasPrefix:#"<UIPeripheralHostView"]) {
for (UIView* peripheralView in [possibleFormView subviews]) {
// hides the backdrop (iOS 7)
if ([[peripheralView description] hasPrefix:#"<UIKBInputBackdropView"]) {
//skip the keyboard background....hide only the toolbar background
if ([peripheralView frame].origin.y == 0){
[[peripheralView layer] setOpacity:0.0];
}
}
// hides the accessory bar
if ([[peripheralView description] hasPrefix:#"<UIWebFormAccessory"]) {
// remove the extra scroll space for the form accessory bar
UIScrollView *webScroll;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
webScroll = [[self webviewpot] scrollView];
} else {
webScroll = [[[self webviewpot] subviews] lastObject];
}
CGRect newFrame = webScroll.frame;
newFrame.size.height += peripheralView.frame.size.height;
webScroll.frame = newFrame;
// remove the form accessory bar
[peripheralView removeFromSuperview];
}
// hides the thin grey line used to adorn the bar (iOS 6)
if ([[peripheralView description] hasPrefix:#"<UIImageView"]) {
[[peripheralView layer] setOpacity:0.0];
}
}
}
}
}
Not easily. You could try to go poking around the subviews in the web view but it would be taboo with Apple.
How about not putting the text field in the web page on the web side, and adding your textfield/textview to the webview explicitly so it doesn't show the nav bar at all, and you can add your own from scratch?
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
-(void)keyboardWasShown:(NSNotification*)aNotification
{
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:#"<UIPeripheralHostView"] == YES)
{
keyboard.hidden = YES;
UIView* keyboardLayer;
for(int n = 0; n < [keyboard.subviews count]; n++)
{
keyboardLayer = [keyboard.subviews objectAtIndex:n];
NSLog(#" keyboardLayer ::: %# " ,keyboardLayer);
if([[keyboardLayer description] hasPrefix:#"<UIWebFormAccessory"] == YES)
{
[keyboardLayer removeFromSuperview ];
}
}
keyboard.hidden = NO;
}
}
}
NSLog(#"keyboardWasShown" );
}
check this as well: http://pastebin.com/s3Fkxvsk