Dismiss keyboard when default ios mail app opens - ios

In my app I display the MFMailComposeViewController to display default mail app. How to dismiss the keyboard which opens up in the controller?

UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
I hope This Will Help You.. :)

Unfortunately it look's like even if we try to force close the keyboard using endEditing; that function takes no effect. On my device it doesn't seem like the keyboard can be dismissed.
Below would have been the answer that should have worked:
Display the MFMailComposeViewControllerwith a completion and call endEditing: on the MFMailComposeViewController's view.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
UPDATE:
Wouldn't suggest doing this but, here is a hackish way that works:
Objective-C:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
UITextField *a = [[UITextField alloc] init];
[mailVC.view addSubview:a];
[a becomeFirstResponder];
[mailVC.view endEditing:YES];
}];
Swift:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}

In your viewcontroller.h add:
#property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
Also, add this function in the .m file:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}

Related

Navigation Bar's Button Item Not Clickable on iPad

In my app, I use the old GKImage library for adding pictures and being able to custom crop them easily. This works pretty well, except when it comes to iPad. On iPhone, everything works great. The main issue is this:
When I use an iPad, none of the barButtonItems are clickable.
Here is my code for this view:
- (void)_actionCancel{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)_actionUse{
NSLog(#"PRESSED");
_croppedImage = [self.imageCropView croppedImage];
[self.delegate imageCropController:self didFinishWithCroppedImage:_croppedImage];
}
- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(_actionCancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Use"
style:UIBarButtonItemStylePlain
target:self
action:#selector(_actionUse)];
}
- (void)_setupCropView{
self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.view addSubview:self.imageCropView];
}
#pragma mark -
#pragma Super Class Methods
- (id)init{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Choose Photo";
[self _setupNavigationBar];
[self _setupCropView];
[self.navigationController setNavigationBarHidden:NO];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.imageCropView.frame = self.view.bounds;
}
The code used to present this controller is:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(280, 280);
self.imagePicker.delegate = self;
self.imagePicker.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
Is it an issue with presenting the view controller?

Custom Views aren't dismissing keyboards?

Have tried [[self jotNotes] resignFirstResponder]; , have tried [self endEditing:YES];
So I have my NoteViewController, which inherits UIViewController, and tries to implement the delegate like so
#interface NOTEController : UIViewController <UITextViewDelegate>
#end
#implementation NOTEController
-(id)init {
self = [super init];
if (self) {
// self.delegate = self; //doesnt let me set this, so i assume i do not do that here
NOTEControllerView * mainView = [[NOTEControllerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = mainView; //just a plain custom uiview subclass its boring and not special
}
return self;
}
#end
and then in the mainView, i have a bunch of sub-views that are basically a square with a UITextView inside.
the squares class is like this, and they're the ones im trying to dismiss the keyboard from, heres where i set the delegate, the dismissKB method, and the UITextView code. Currently, it will log my keyboard method when pressing the done button, but the keyboard is still present. Would really appreciate if anyone could help me understand why
#interface NOTESubview : UIView <UITextFieldDelegate>
#property (nonatomic, weak) id<UITextFieldDelegate> delegate;
-(UITextView *)jotNotes;
#end
#implementation NOTESubview
-(id)initWithFrame:(CGRect)arg1 {
self = [super initWithFrame:arg1];
if (self) {
self.delegate = self;
[self addSubview:[self jotNotes]];
}
return self;
}
-(UITextView *)jotNotes {
UITextView * jotNotes = [[UITextView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/5.7, self.frame.size.width, self.frame.size.height - self.frame.size.height/5.7)];
UIToolbar* keyboardTextViewBar = [[UIToolbar alloc] init];
keyboardTextViewBar.barStyle = UIBarStyleDefault;
[keyboardTextViewBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(dismissKB:)];
[keyboardTextViewBar setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
jotNotes.inputAccessoryView = keyboardTextViewBar;
jotNotes.delegate = self.delegate;
return jotNotes;
}
-(void)dismissKB:(UIBarButtonItem *)sender {
//this will log, so im not sure why it wont resign the board no matter what i try
NSLog(#"keyboard attempted to dismiss by %#", sender);
[[self jotNotes] resignFirstResponder];
}
I suspect that when the dismissKB method is called, its actually not the one who is currently the first responder.
However, there's a trick where you can just "dismiss the keyboard" from anywhere in your app. You might wanna give it a try:
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
Add the following line where you are trying to dismiss the keyboard:
[self endEditing:YES];

Unable to get touch on my UIImageView - iOS

I need to make my every single UIImageView to be single tapped.
I have a viewController, on this view controller there is a view which i push another views on this view with containment.
I am trying to create gallery ,
What might I be doing wrong?
This is my code;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Custom initialization
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[self addGestureRecognizer:singleTap];
[self setBackgroundColor:[UIColor lightGrayColor]];
}
return self;
}
-(void)openPhotoDetail
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SideMenu" bundle:[NSBundle mainBundle]];
PhotoDetailViewController *photoDetailViewController = [storyboard instantiateViewControllerWithIdentifier:#"photoDetailViewController"];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.window.rootViewController.menuContainerViewController.centerViewController pushViewController:photoDetailViewController animated:YES];
}
Initially a UIImageView has user interaction disabled. Enalbe the user interaction of your UIImageView and it should work.
imageView.userInteractionEnabled = YES;
In Viewdidload set
YourImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openPhotoDetail:)];
// Instead of self ,give your imageView to add gesture event
[YourImageView addGestureRecognizer: singleTap];
Click on the UIImageView, on storyboard,
Go to Attributes Inspector (The button that looks like a tuxedo, top right ,Second button).
Check the attribute "User Interaction Enabled"
Or if you want to do it by code,
in
-(void)viewDidLoad {
myImageView1.userInteractionEnabled = YES;
myImageView2.userInteractionEnabled = YES;
myImageView3.userInteractionEnabled = YES;
}
UIImageView *MyImageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYZ.png"]];
UITapGestureRecognizer *TapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[MyImageview addGestureRecognizer:TapGesture];
[MyImageview setUserInteractionEnabled:YES];
Can u use it like that

modalPresentationStyle from button click

I have a _exampleButton:
_exampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_exampleButton setBackgroundColor:[UIColor redColor]];
[_exampleButton addTarget:self action:#selector(certificatesButtonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_exampleButton];
and the action:
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
[_certificatesWindow release];
}
else {
[_certificatesWindow.view removeFromSuperview];
[_certificatesWindow release];
_certificatesWindow = nil;
}
}
this presents the window in modal view controller from other class:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelItem;
UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
[self.view addSubview:table];
_tableFromButton = table;
}
- (void)cancel:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
but after clicking the Cancel button, the Modal view controller view disappears, but if I click the _exampleButton again once- it will not appear, so I have to click it twice to show the modal view controller again. What is the problem?
It does this because that is what your code tell it to do...
You dismiss the view controller in -(void)cancel:(id)sender but this will not result in the _certificatesWindow iVar becoming nil in your first view controller. So when you touch the exampleButton again it will execute the else clause and clean up the _certificatesWindow view controller.
You should either use delegation or a code block to have the first view controller dismiss the second or remove the if/else test from your certificatesButtonTouched. Another alternative is to modify this method so that if _certificatesWindow is not nil it is reused -
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
}
But this may or may not be desirable depending on what the second view controller shows.
I would also suggest you look at converting to ARC if possible
The problem is after you tap "Cancel", _certificatesWindow still exists when you tap on certificatesButtonTouched. It will go to the Else statement on the First tap to make _certificatesWindow = nil.
if _certificatesWindow is not needed when you tap cancel, you might want to do the following:-
- (void)cancel:(id)sender
{
[self.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
[_certificatesWindow.view removeFromSuperview];
[_certificatesWindow release];
_certificatesWindow = nil;
}
allocate view like this in view did load method
if(!_certificatesWindow)
{
_certificatesWindow = [[ViewController1 alloc] init];
}
-(void)certificatesButtonTouched
{
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
}
replace your code with this
-(void)certificatesButtonTouched
{
if(!_certificatesWindow)
{
_certificatesWindow = [[AWCertificatesViewController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_certificatesWindow];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
[_certificatesWindow release];
}

ABPeoplePickerNavigationController change Cancel button text to Done and still call delegate method?

If the text itself can't be changed and the button needs to be replaced then please also explain how to make the delegate method
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
still get called from replacement button?
Any help is greatly appreciated.
After some fiddling this worked:
Include UINavigationControllerDelegate in object .h file. Add the following to .m file:
- (void) showAddressBook {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// use delegate to change cancel text to done on picker that pops up
picker.delegate = self;
[self presentViewController:picker animated:true completion:nil];
}
// replace button now that controller is initialized
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UIBarButtonItem *obbi = navigationController.topViewController.navigationItem.rightBarButtonItem;
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:obbi.target action:obbi.action];
navigationController.topViewController.navigationItem.rightBarButtonItem = bbi;
}
}

Resources