I want to dismiss keyboard by click dismiss button ,How to build a bar on the keyboard? like this:
(source: alexcurylo.com)
It was answered here. Basically, you send a resignFirstResponder message to the UITextView. Of course, you will put that code on your button's delegate.
Try to put following code. It might work for you.
Put following variables in your viewController.h file
UIToolbar *keyboardToolbar;
id notisender;
BOOL isAlreadyResigned;
float kx,ky,kh,kw;
Now put following code in you viewController.m file.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
isAlreadyResigned=YES;
[self keyboardToolbarShouldShow];
[self keyboardToolbarShouldShow];
[super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[self keyboardToolbarShouldNotShow];
[super viewWillDisappear:animated];
}
#pragma mark keyboardWillShow methods
-(void)keyboardToolbarShouldShow {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardhide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardToolbarShouldNotShow {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
if(!isAlreadyResigned){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillNotShow:)
name:UIKeyboardWillShowNotification
object:nil];
[GEEtxtMsg becomeFirstResponder];
}
}
-(void)keyboardWillShow : (NSNotification *)sender {
#try {
// NSLog(#"notification in first view");
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(#"keyboard description - %#",[keyboard description]);
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
CGRect kbBounds = [v CGRectValue];
if(keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
keyboardToolbar.barStyle=UIBarStyleBlackOpaque;
keyboardToolbar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissKeyboard)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [[NSArray alloc] initWithObjects:flex, barButtonItem, nil];
[keyboardToolbar setItems:items];
[items release];
}
[keyboardToolbar removeFromSuperview];
keyboardToolbar.frame = CGRectMake(0, 0, kbBounds.size.width, 45);
[keyboard addSubview:keyboardToolbar];
NSLog(#"x=%f y=%f width=%f height=%f",kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height);
kx=kbBounds.origin.x; ky=kbBounds.origin.y;
kh=kbBounds.size.height; kw=kbBounds.size.width;
keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 87);
isAlreadyResigned=NO;
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 45, kbBounds.size.width, kbBounds.size.height);
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)keyboardWillNotShow : (NSNotification *)sender {
#try {
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
// CGRect kbBounds = [v CGRectValue];
if([[keyboard subviews] containsObject:keyboardToolbar]){
[keyboardToolbar removeFromSuperview];
}
if(!isAlreadyResigned){
isAlreadyResigned=YES;
keyboard.bounds = CGRectMake(kx,ky,kw,kh);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(0, 0,0, 0);
}
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)dismissKeyboard {
[self resignTextFields];
}
-(void)keyboardhide:(NSNotification *)noti {
notisender = noti;
}
Here you can build a bar on a keyboard
Take static toolbar on xib and take one button and put it on toolbar, and make toolbar hidden ,now
write code in your method when you create a toolbar ,like if you want to create toolbar on textview begin editing method then,
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.26];
[tlBar setFrame:CGRectMake(0, 220, 320, 44)];
tlBar.hidden=NO;
[UIView commitAnimations];
}
And when you dismiss keyboard write this method and call this method when toolbar button pressed
-(IBAction)kbAway:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[tlBar setFrame:CGRectMake(0, 480, 320, 44)];
[UIView commitAnimations];
[txtviewmessage resignFirstResponder];
[txtsubject resignFirstResponder];
[txttemplatename resignFirstResponder];
}
Hope this will help you..
Related
I have a little problem with the notific of keyboard disappear, I add the observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
The add the method:
- (void) keyboardDidDisappear:(NSNotification *)notification {
NSLog(#"disappear");
}
In my application I have some button, on click it show two uitextfield with this method:
- (void) showFieldStoryView:(id)sender {
if (storyContentView.hidden == NO) {
UIButton *button = (UIButton *)sender;
buttonTag = [button tag];
int indexArray = buttonTag - 1;
NSMutableDictionary *dict = [arrayPunteggi objectAtIndex:indexArray];
[fieldUno removeFromSuperview];
[fieldDue removeFromSuperview];
[fieldUno setHidden:YES];
[fieldDue setHidden:YES];
[storyContentView addSubview:fieldUno];
[storyContentView addSubview:fieldDue];
[fieldUnoAccusi removeFromSuperview];
[fieldDueAccusi removeFromSuperview];
[fieldUnoAccusi setHidden:YES];
[fieldDueAccusi setHidden:YES];
[labelPunti removeFromSuperview];
[labelAccusi removeFromSuperview];
[labelPunti setHidden:YES];
[labelAccusi setHidden:YES];
[storyContentView addSubview:labelPunti];
[storyContentView addSubview:labelAccusi];
if (fieldUno.isFirstResponder) {
point = CGPointMake(0,button.frame.origin.y);
}
else {
[fieldUno becomeFirstResponder];
}
}
}
the problem is that every time I click on this button I see on console the log disappear, and the strange thins is that the keyboard is always show oh the screen, where is the problem?
I present one controller to play video:
[self presentModalViewController:movieController animated:YES];
and add an observer when finish palying:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedPlayback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
-(void) movieFinishedPlayback:(NSNotification*)notification{
NSLog(#"........movieFinishedPlayback....... \n ");
[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
This my VideoDetailViewController:
#import "VideoDetailViewController.h"
#import "PSStackedView.h"
#import "YunMaoIpadAppDelegate.h"
#define IpadAppDelegate ((YunMaoIpadAppDelegate *)[[UIApplication sharedApplication] delegate])
#define IphoneAppDelegate ((YunMaoIosAppDelegate *)[[UIApplication sharedApplication] delegate])
#interface VideoDetailViewController ()
#end
#implementation VideoDetailViewController
#synthesize video, moviePlayer, collectionsArray;
-(id)initWithVideo:(Video *)theVideo
{
self = [super initWithNibName:#"VideoDetailViewController" bundle:nil];
if (self) {
self.video = theVideo;
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[moviePlayer pause];
//[self.navigationController setNavigationBarHidden:false animated:animated];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated;
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[super viewDidDisappear:animated];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
}
- (void) viewDidAppear:(BOOL)animated
{
//[moviePlayer play];
//[DejalBezelActivityView removeViewAnimated:YES];
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(displayActivityView) withObject:self.moviePlayer.view afterDelay:0.1];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:video.link]];
[moviePlayer prepareToPlay];
//[moviePlayer setShouldAutoplay:NO];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if([moviePlayer respondsToSelector:#selector(useApplicationAudioSession)])
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSLog(#"iPhone ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 480.0f, 320.0f)];
//moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
}
else{
NSLog(#"iPad ios5.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
else
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
NSLog(#"iPhone ios6.x");
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
}
if(result.height == 568)
{
//moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer.view setFrame:self.view.bounds];
}
}
else{
NSLog(#"ipad ios 6.x");
[moviePlayer.view setFrame: CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f)];
//[moviePlayer.view setFrame:[IpadAppDelegate window].bounds];
}
}
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
- (BOOL)shouldAutorotate{
return NO;
}
/*
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationLandscapeRight;
}
*/
- (IBAction)displayActivityView
{
[DejalBezelActivityView activityViewForView:self.moviePlayer.view withLabel:#"节目正在下载中,请稍后..."].showNetworkActivityIndicator = NO;
}
- (void)removeActivityView;
{
[DejalBezelActivityView removeViewAnimated:YES];
[[self class] cancelPreviousPerformRequestsWithTarget:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This worked for me.
[self dismissViewControllerAnimated:NO completion:^{
[self.view removeFromSuperview];
}];
Hope it helps!
Before dismissing movieplayer do [moviePlayer stop]; and remove movieplayer.view from superview. Try dismissing after that. That might fix the issue.
I have tried the following code from another application but it doesn't find anything. Why? How to get access to the UIWebView toolbar?
- (UIToolbar *)findVirginWebKeyboardToolbar:(UIView *)parent
{
if ([parent isKindOfClass:[UIToolbar class]]) {
UIToolbar *tb = (UIToolbar *)parent;
if ([tb.items count] == 1 && [((UIBarButtonItem *)[tb.items objectAtIndex:0]).customView isKindOfClass:[UISegmentedControl class]]) {
return tb;
}
}
for (UIView *view in parent.subviews) {
UIToolbar *tb = [self findVirginWebKeyboardToolbar:view];
if (tb) return tb;
}
return nil;
}
- (void)removeKeyboardBar {
UIView *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
keyboardWindow = testWindow;
UIToolbar *toolbar = [self findVirginWebKeyboardToolbar:keyboardWindow/*subviewWhichIsPossibleFormView*/];
if (toolbar) {
itemsArray = [NSArray arrayWithObjects:button1, button2, button3, nil];
[toolbar setItems:itemsArray];
}
}
}
With the following code you should be able to remove the toolbar that is above the keyboard.
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
[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 *formView in [keyboardWindow subviews]) {
// iOS 5 sticks the UIWebFormView inside a UIPeripheralHostView.
if ([[formView description] rangeOfString:#"UIPeripheralHostView"].location != NSNotFound) {
for (UIView *subView in [formView subviews]) {
if ([[subView description] rangeOfString:#"UIWebFormAccessory"].location != NSNotFound) {
// remove the input accessory view
[subView removeFromSuperview];
}
else if([[subView description] rangeOfString:#"UIImageView"].location != NSNotFound){
// remove the line above the input accessory view (changing the frame)
[subView setFrame:CGRectZero];
}
}
}
}
}
here is problem, when i debug my program, i found keyboardWillShow function not responds every time. just first time, it will be called by program. here is my code, i dont know whats wrong in my code, but, when the keyboard first appeared, the function run well.
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.textview.frame;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
textview.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
// textview.frame = self.view.bounds;
[self save];
[UIView commitAnimations];
}
and i regist notification
- (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];
}
and remove it in here
- (void)viewDidUnload
{
[super viewDidUnload];
[self save];
self.textview = nil;
self.title = nil;
self.tags = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
i resign firstresponder, here is my code
- (IBAction)save:(id)sender {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNote)] autorelease];
[textview resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(save:)] autorelease];
[self setUpUndoManager];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[self save];
return YES;
}
Make sure that you are not writing any code to remove observer.....
Please provide keyboardWillHide method also....
how can i programmatically show/hide this opaque view from UISearchDisplayController?
Probably in searchDisplayControllerWillBeginSearch or searchDisplayControllerDidBeginSearch i need to set something... but what?
thanks.
Temporary solved using UIKeyboardWillAppearNotification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
OpaqueView is an UIControl with alpha = 0.8.
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if( [subview isKindOfClass:[UIControl class]] ) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
I used this ORRIBLE way to temporary fix problem.... any other idea will be appreciated!
thanks.
Code given by elpsk is current but will not work in iOS7 and above
Code working in both iOS6 and iOS7 is as below
- add below notification in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
Write below function
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:#"UISearchDisplayControllerContainerView"])) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
NOTE : Code just have one extra condition as in iOS7 UIControl class become UISearchDisplayControllerContainerView,
The other answers where not working for me. This one works for me on iOS7 and iOS8.
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:#"UISearchDisplayControllerContainerView"])) {
for(UIView *subView2 in subview.subviews)
{
for(UIView *subView3 in subView2.subviews)
{
if (subView3.alpha < 1) {
subView3.hidden = YES;
}
}
}
}
}
If you don't need support for iOS7 please don't use the searchDisplayController anymore because its deprecated. For iOS8 use the UISearchController and the dimsBackgroundDuringPresentation
Property
Ref: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/instp/UISearchController/dimsBackgroundDuringPresentation
Mmmm...quick answer. Not pretty but surely works
#pragma mark UISearchBarDelegate
// Displays a view to simulate the lose of focus
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
UIButton *view1 = [[UIButton alloc] init];
view1.frame = CGRectMake(0, 0, 320, MAX(480, self.tableView.contentSize.height));
view1.alpha = 0.6;
view1.tag = 2000;
view1.backgroundColor = [UIColor blackColor];
[view1 addTarget:self
action:#selector(removeView)
forControlEvents:UIControlEventTouchUpInside];
[self.tableView setScrollEnabled:NO];
[self.tableView addSubview:view1];
[view1 release];
}
/**
* Pop the view and the keyboard
*/
- (void)removeView {
UIView *v = [self.tableView viewWithTag:2000];
v.hidden = YES;
[v removeFromSuperview];
[self.tableView setScrollEnabled:YES];
[self.searchBar resignFirstResponder];
}
That view is showed when you're writing, so I guess you should use it at searchBarTextDidBeginEditing. If I'm wrong, use it when you start searching or whatever.