how to hide keyboard when tap background [duplicate] - ios

This question already has answers here:
iOS - Dismiss keyboard when touching outside of UITextField
(38 answers)
Closed 8 years ago.
I have one TableViewCell that has UITextField in it.
I want when click out of UITextField hidden keyboard but I don't about that.
this is my code:
#implementation TextFieldCell
#synthesize Textfield,placeholder;
- (void)awakeFromNib
{
// Initialization code
Textfield = [self makeTextField];
[self addSubview:Textfield];
placeholder = [self PlaceHolderLabel];
[self addSubview:placeholder];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UITextField *)makeTextField
{
CGRect TextFrame = CGRectMake(0,0,300,80);
UITextField *textfield = [[UITextField alloc]initWithFrame:TextFrame];
textfield.delegate = self;
textfield.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
textfield.backgroundColor = [UIColor yellowColor];
textfield.placeholder = [NSString stringWithFormat:#"Mamal"];
UIView *spacerleftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
UIView *spacerrightView = [[UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
[textfield setLeftViewMode:UITextFieldViewModeAlways];
[textfield setRightViewMode:UITextFieldViewModeAlways];
textfield.leftView = spacerleftView;
textfield.rightView = spacerrightView;
return textfield;
}
- (UILabel *)PlaceHolderLabel{
NSString *labelText = Textfield.placeholder;
Textfield.placeholder = nil;
CGRect labelFrame = CGRectMake(0,0,280,80);
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
[placeholderLabel setTextColor:[UIColor lightGrayColor]];
[placeholderLabel setText:labelText];
placeholderLabel.textAlignment = NSTextAlignmentRight;
return placeholderLabel;
}
- (UILabel *)clearPlaceHolderLabel{
NSString *labelText = nil;
CGRect labelFrame = CGRectMake(0,0,280,80);
UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
[placeholderLabel setText:labelText];
return placeholderLabel;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
//perform actions.
NSLog(#"start");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"end");
}

Add gesture recogniser in viewDidLoad :
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard:)];
[self.view addGestureRecognizer:tap];
and then implement closeKeyboard:
- (IBAction)closeKeyboard:(id)sender {
[self.view endEditing:YES];}

my friend I had this problem but I can solve that.
you don't need add UITextField in TableViewCell , you can create TextField in ViewController or TableViewController that your tableView is inner it and then add TextField inside any cell that you want.
like this code:
ViewController.h
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
#property (nonatomic,strong) UITableView *table;
#property (weak,nonatomic) UITextField *Textfield;
ViewController.m
#implementation ViewController
#synthesize table,Textfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//add table in view
table = [self makeTableView];
[self.view addSubview:table];
[self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
//run textfield programmatically
Textfield = [self makeTextField];
//hide keyboard with hideKeyboard selector
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.table addGestureRecognizer:gestureRecognizer];
}
- (void) hideKeyboard {
[Textfield resignFirstResponder];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}

Try to use
[self.view setEditing:YES];
in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

try this:
// somewhere in viewDidLoad
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnBackground)];
[self.view addGestureRecognizer:tapRecognizer];
self.view.userInteractionEnabled = YES;
// ...
- (void)tapOnBackground
{
[self.view endEditing:YES];
}

Add this code in your textField implementation method.
//Set to Remove Keyboard from view
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
After that create dismissKeyboard method.
//First Responder Remove Keyboard
-(void)dismissKeyboard {
[Textfield resignFirstResponder];
}

Related

A TapGestureRecognizer was added into a Label,but it doesn't work

I need a label to receive single tap touch, so I wrote the following code:
#import "ViewController.h"
#interface ViewController ()
{
UILabel* label;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
label=[[UILabel alloc]initWithframe:CGRectMake(15 30 350 300)];
label.backgroundColor=[UIColor greenColor];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTapOnLabel:)];
[tap setNumberOfTapsRequired:1];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
}
- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture
{
NSLog(#"receive tap.");
}
Unluckly,the tapGesture cannot be recognized. Could any one help me with this strange issue? Thank you so much.
A label's user interaction is disabled by default:
label.userInteractionEnabled = YES

Objective C Subclass UITextField to copy only

I'm trying to create a class that makes a read only text field that allows the user to copy its contents. Here is my code:
CopyOnly.h
#import <UIKit/UIKit.h>
#interface CopyOnly : UITextField
#end
CopyOnly.m
#import "CopyOnly.h"
#implementation CopyOnly
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self attachTapHandler];
}
return self;
}
- (void) attachTapHandler
{
[self setUserInteractionEnabled:YES];
UIGestureRecognizer *touchy = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self addGestureRecognizer:touchy];
}
- (BOOL) canPerformAction: (SEL) action withSender: (id) sender
{
return (action == #selector(copy:));
}
- (void) handleTap: (UIGestureRecognizer*) recognizer
{
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
- (void)copy:(id)sender
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
#end
This works great, only the keyboard is coming up. I don't want any keyboard to come up.
I tried adding this to initWithFrame:
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
This does not give me the result I'm expecting. Anyone know how I can do this?
Extending on my comment. This is easily accomplished using a UITextView (not UITextField) with editable property set to NO.
UITextView* tf = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
tf.editable = NO;
tf.text = #"Hey this is a test!";
[self.view addSubview:tf];
Adding this to -(BOOL)canBecomeFirtResponder seemed to do the trick. Hacky, but it works.
- (BOOL) canBecomeFirstResponder
{
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
return YES;
}
If you stuck with text field that doesn't allow editing try totally different approach. Follow instructions in this article http://nshipster.com/uimenucontroller/ to implement UILabel that supports copying.

Showing TextView and Typing Text in TextView if i click any places in ImageView

I want to show TextView and I want to type the text in the TextView.
Whenever I click or Touch any place in the ImageView.
Here actually I set the UITapGestureRecognizer for TextView.
Also once I type the text in the TextView it should be a Automatic saveable.
Here I set the ImageView.After that i set the SubView and inside the SubView i set ImageView for Zoom.
Now I get the Zoom image. Exactly if I click or Touch any places in the Zoom ImageView,it should show the TextView as well as once I Type Text in the TextView,it should be a Automatic saveable.
But I can't get the TextView, whenever I click or Touch in the Zoom ImageView.
How can I get that?
Below .h part
#import <UIKit/UIKit.h>
#interface GalleryCameraBusinessViewController : UIViewController<UIImagePickerControllerDelegate,UITextViewDelegate,UIGestureRecognizerDelegate>
{
UIView *dynamicView;
UIImageView *dynamicImage;
UITextView *textView;
UITextView * textviewtext;
}
- (IBAction)backtobusinesscard:(id)sender;
#property (strong, nonatomic) IBOutlet UISwitch *swit;
- (IBAction)switchaction:(id)sender;
#property (strong, nonatomic) IBOutlet UIImageView *pickingimageofcamgal;
#property (strong, nonatomic) IBOutlet UITextField *enternametextfld;
#end
.m Part
#import "GalleryCameraBusinessViewController.h"
#interface GalleryCameraBusinessViewController ()
#end
#implementation GalleryCameraBusinessViewController
#synthesize swit,pickingimageofcamgal,enternametextfld;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self tapdetected];
}
- (void)viewWillAppear:(BOOL)animated
{
UITapGestureRecognizer *tapgesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected)];
tapgesture.numberOfTouchesRequired =1;
tapgesture.numberOfTapsRequired =1;
pickingimageofcamgal.userInteractionEnabled =YES;
[pickingimageofcamgal addGestureRecognizer:tapgesture];
UITapGestureRecognizer *tapgesture1 =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected1)];
tapgesture1.numberOfTapsRequired =1;
tapgesture1.numberOfTouchesRequired =1;
pickingimageofcamgal.userInteractionEnabled =YES;
[pickingimageofcamgal addGestureRecognizer:tapgesture1];
UITapGestureRecognizer *tapgesture2 =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected2)];
tapgesture2.numberOfTouchesRequired =1;
tapgesture2.numberOfTapsRequired =1;
pickingimageofcamgal.userInteractionEnabled=YES;
[pickingimageofcamgal addGestureRecognizer:tapgesture2];
// UITapGestureRecognizer *tapgesture3 =[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected3)];
// tapgesture3.numberOfTapsRequired =1;
// dynamicView.userInteractionEnabled =YES;
// [dynamicView addGestureRecognizer:tapgesture3];
UITapGestureRecognizer *tapgesture4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected4)];
tapgesture4.numberOfTapsRequired=1;
tapgesture4.numberOfTouchesRequired =1;
tapgesture4.delegate =self;
dynamicImage.userInteractionEnabled =YES;
[dynamicImage addGestureRecognizer:tapgesture4];
NSLog(#"textview is==%#",textviewtext);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backtobusinesscard:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)tapdetected
{
UIImagePickerController*picker =[[UIImagePickerController alloc]init];
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate =self;
[self presentViewController:picker animated:NO completion:nil];
}
-(void)tapdetected1
{
UIImagePickerController *picker =[[UIImagePickerController alloc]init];
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
picker.delegate =self;
[self presentViewController:picker animated:NO completion:nil];
}
-(void)tapdetected2
{
//For dynamically creating view
dynamicView =[[UIView alloc]initWithFrame:CGRectMake(10, 70, 280, 300)];
dynamicView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:dynamicView];
//For dynamically creating imageview
dynamicImage =[[UIImageView alloc]init];
dynamicImage.frame=CGRectMake(10, 60, 280, 300);
[dynamicImage setUserInteractionEnabled:YES];
dynamicImage.image =pickingimageofcamgal.image;
[dynamicView addSubview:dynamicImage];
}
//-(void)tapdetected3
//{
// textviewtext =[[UITextView alloc]initWithFrame:CGRectMake(20, 120, 120, 53)];
// textviewtext.backgroundColor=[UIColor redColor];
// [textviewtext setDelegate:self];
// textviewtext.text=#"Welcome to textview";
// [dynamicView addSubview:textviewtext]; +
//}
-(void)tapdetected4
{
textviewtext =[[UITextView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
textviewtext.backgroundColor = [UIColor blueColor];
[textviewtext setDelegate:self];
textviewtext.text=#"HI";
[dynamicImage addSubview:textviewtext];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
pickingimageofcamgal.image =[info objectForKey:UIImagePickerControllerOriginalImage];
picker.delegate=self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)switchaction:(id)sender
{
if(swit.isOn)
{
[self tapdetected];
}
else
{
[self tapdetected1];
}
}}
actually you need to modify the some lines because you were created the tapgesture4 in before allocation of dynamic view, that the reason it showing at null, now u simply do a simple change, just convert the tapgesture4 into tapdetected2 method now check, it work surely fine for u.
-(void)tapdetected2
{
//For dynamically creating view
dynamicView =[[UIView alloc]initWithFrame:CGRectMake(10, 70, 280, 300)];
dynamicView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:dynamicView];
//For dynamically creating imageview
dynamicImage =[[UIImageView alloc]init];
dynamicImage.frame=CGRectMake(10, 60, 280, 300);
[dynamicImage setUserInteractionEnabled:YES];
dynamicImage.image =pickingimageofcamgal.image;
[dynamicView addSubview:dynamicImage];
UITapGestureRecognizer *tapgesture4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapdetected4)];
tapgesture4.numberOfTapsRequired=1;
tapgesture4.numberOfTouchesRequired =1;
tapgesture4.delegate =self;
dynamicImage.userInteractionEnabled =YES;
[dynamicImage addGestureRecognizer:tapgesture4];
NSLog(#"textview is==%#",textviewtext);
}

Not getting button action/ Single tap on custom MKannotation call out view

I have created a custom annotation and a call out for my map view. I need to navigate to another view when the user clicks on call out view or he clicks to the button that added as sub view to the callout view. But both gesture recognizer and add target is not working for me in this case. The setSelected: method was invoked and the view get hidden when tap occurs in call out view.
#interface VBPunchCardAnnotation : MKAnnotationView{
UIView *calloutView;
}
- (id)initWithAnnotation:(id )annotation reuseIdentifier:(NSString *)reuseIdentifier deal:(id)punchdeal
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
calloutView = [[UIView alloc] init];
calloutView.hidden = YES;
infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[calloutView addSubview:infoButton];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(annotationTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[calloutView addGestureRecognizer:singleTap];
[infoButton addTarget:self action:#selector(annotationTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:calloutView];
return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// show/hide callout and swap pin image
calloutView.hidden = !selected;
self.image = !selected ? normalPin : selectedPin;
// dispatch an event to alert app a pin has been selected
if(selected) [[NSNotificationCenter defaultCenter] postNotificationName:#"punchCardAnnotation" object:self];
}
-(void)annotationTapped:(id)sender{
[self.delegate punchCardAnnotationClickedForDeal:self.punchDeal];
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if ([hitView isKindOfClass:[UIButton class]]) {
}
}
Finally I got the answer. It;s here
Followed this tutorial. Really great solution.
https://github.com/nfarina/calloutview
Happy coding!!

change UITextField BackGround Color by LongPress

All Boxes is UITextFields. I want change background color when user long press on UITextField.
Which TextField had a long press that UITextField color is change, not all UITextFields.
Try to use like this...
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gs = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(changeBackground:)];
[textFld addGestureRecognizer:gs];
}
- (void)changeBackground:(UIGestureRecognizer *)gs
{
[self.view endEditing:YES]; // Edited
UITextField *txtFld = (UITextField *)gs.view;
[txtFld setBackgroundColor:[UIColor redColor]];
}
You can use UILongPressGestureRecognizer. Please note that one gesture recognizer can be attached to one view.
Here is code example
- (void)viewDidLoad
{
[super viewDidLoad];
//Array that holds your textfields
NSArray *myTextFields;
for (UITextField *textField in myTextFields) {
//Creating UILongPressGestureRecognizer
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
//Attaching it to textfield
[textField addGestureRecognizer:longPressGestureRecognizer];
}
}
//Handling long press
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
UITextField *textField = (UITextField *)gestureRecognizer.view;
textField.backgroundColor = [UIColor greenColor];
}

Resources