UITextField show cursor even when userInteractionEnabled is set to NO - iphone

I have a UITextField and it must blink the cursor anyway, even its userInteractionEnabled property is set to NO. I don't want the UITextField to becomeFirstResponder and show the keyboard.
Now you may be asking:
1. If you want to hide the keyboard, why to show the cursor?
A: The problem is that I need to show the user the UITextField is being edited, with a different/custom keyboard.
2. So why don't you use the inputView property?
A: Because the keyboard in inputView comes up from bottom, and I want my custom keyboard in the center of the screen.
So let's go to the real question:
How can I show the cursor? Is there any property I can set? If not, how I would draw a cursor? Making a UIView that gets added and removed with alpha, or subclassing UITextField and overriding drawInRect?

You can add a small UIView with a repeting animation that sets the alpha to 0 and 1 back and forth with animateWithDuration:delay:options:animations:completion:.
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionRepeat
animations:^{ [cursorView setAlpha:0]; }
completion:^(BOOL animated){ [cursorView setAlpha:1]; } ];
To position the view correctly as a subview of the textfield, in front of the text entered, you can use the NSString method sizewithfont:forWidth:lineBreakMode:

Related

UIView not animating the first time

This code animates my view every time it's run, except for the first time. When the keyboard is displayed or hidden, a UIView is repositioned:
[UIView
animateWithDuration:0.26
animations:^{
[self setupActiveOverlayViewFrame];
}completion:nil];
-(void)setupActiveOverlayViewFrame {
float optimalOverlayHeight = [self.activePanel optimalHeight];
float realOverlayHeight = MIN(optimalOverlayHeight, self.displayView.frame.size.height);
if (self.activePanel.frame.size.height != realOverlayHeight) {
self.activePanel.frame = CGRectMake(self.activePanel.frame.origin.x, 0, self.activePanel.frame.size.width, realOverlayHeight);
}
self.activePanel.center = [self correctCenterForOverlay];
}
The method I posted is just to show that all it does is re-size and re-position it.
The first time this code is run, it doesn't animate. It just jumps into position. Every time after that, it animates correctly.
Its possible that the "keyboard did display" notification is being called before your view is fully set up, like if you've pushed a view controller onto a navigation controller and immediately give a text view focus on viewDidLoad
You can either keep track of when the keyboard is up or down and when viewDidAppear is called, check if the keyboard is up, and run it. Or you can postpone giving focus to a text view/field until viewDidAppear: is called.
I got the same problem. I want to advice you to check Panel center before(!) animating to find out has it right position at the first run. If it's not. Just set right position. Also try the next code
[self setupActiveOverlayViewFrame];
[UIView animateWithDuration:0.26
animations:^{
[self.view layoutIfNeeded];
}completion:nil];
P.S. If you are using autolayout better use constraints to move your views using the code above.

NavigationController UIToolbar Change Items

I want to change the items in my UIToolbar by hiding the toolbar, changing the items (button, fixed space, etc), and revealing it again.
I currently have a button on my UIToolbar that, when pressed, hides the toolbar by calling [[self navigationController]setToolbarHidden:YES animated:YES];.
How can I set these items? Is it possible using interface builder or do I need to hard-code them in?
This is non-standard behavior, but should be doable. You might consider instead of removing and adding new buttons to the existing toolbar, to instead create a different toolbar that gets faded in instead. This would make things easier to code/debug. In general, it just requires less "mess."
To achieve the desired behavior, you could do something like:
float animationDuration = .25;
[UIView animateWithDuration:animationDuration animations:*{
// Remove the old toolbar.
self.oldToolbar.alpha = 0;
// Fade the new toolbar in.
self.newToolbar.alpha = 1;
}];
This example assumes that you have already loaded your other toolbar into the newToolbar property. Let me know if you need further assistance or any explanation.
You can set new items for the toolbar this way:
[toolbar setItems:<new_items_array> animated:YES];
It will also animate the change so you may not need to hide and show it again, which is not a good UI practice in general.
A bit of an odd one... this is a bit hacky but should be perfectly fine:
[UIView animateWithDuration:0.5f animations:^{
// Remove the old toolbar.
self.oldToolbar.alpha = 0;
} completion:^(BOOL finished) {
//add code to change toolbar.
[UIView animateWithDuration:0.5f animations:^{
// Fade the new toolbar in.
self.newToolbar.alpha = 1;
}];
}];

Display and hide Textfield programmatically for iPhone/Cocoa Touch

I'm trying to create a custom editor for my UIViewController. It was suggested that I hide the textfields until the user presses the Edit key. How do you do this programmatically?
In other words when the user hits 'Edit' I would like the label to disappear and the Textfield to appear.
Thanks,
You can make use of the hidden property for quickly turning something visible or invisible.
self.widget1.hidden = YES;
self.widget2.hidden = NO;
Another option is to set alpha to 0 to hide and 1 to show. This is beneficial if you want to have an animation fade the widgets in and out for a smooth transition.
[UIView beginAnimations:nil context:NULL];
self.widget1.alpha = 0;
self.widget2.alpha = 1;
[UIView commitAnimations];
You just need to use the hidden property.
label.hidden = YES;
textField.hidden = NO;

How to pull up a keyboard - iphone app

I've dealt with Keyboards with a UITextField but how can I use a Keyboard in the following way:
I have a UITableView
When the user selects a specific row, a keyboard pops up
I'd like to have a method that knows everytime a letter or something is pressed on the keyboard
Place text fields on the cells using the tableView:cellForRowAtIndexPath: method, and give the text field on each cell its own tag to identify it in the table view or text field delegate methods. Optionally, have a cell's text field become first responder when the cell is tapped in tableView:didSelectRowAtIndexPath: (outside of the text field's region).
To make it look like part of the cells, ensure your text fields don't have borders and match the cell background color.
You can hide a tiny (1x1) invisible/transparent text field in the corner somewhere (temporarily make it a subview in the table view cell or on top of the entire table view), make the text field first responder, and trap any character inputs by implementing the text should change delegate and inspecting the replacement text before it's entered.
const CGRect kbottomPickerViewFrame = {{0.0f, 480.0f},{320.0f, 298.0f}};
const CGRect ktopPickerViewFrame = {{0.0f, 480-298},{320.0,298.0f}};
-(void) animateViewUp:(BOOL)up {
[UIView animateWithDuration:.5 animations:^{
if(up) {
self.view.frame = ktopPickerViewFrame;
}else {
self.view.frame = kbottomPickerViewFrame;
}
} completion:nil];
}
use this method it will animate the view . Call it when textField begin editing

keyboard hiding textfields in tableViewController

I am using a tableViewController having 2 sections ....
1st section has 7 rows & 2nd section has 2 rows.
So when i edit in textfields of 2nd section keyboard hides these field so how i will handle
keyboard for these fields only.(i am using tableviewController which has default scrolling).
I am new to objective -C.....Your help please.
Thanks.
On tapping any cell with text field, you can navigate to another view with only a text field, keyboard and Done and Cancel buttons. this feels pretty neat. You can see this in many of apple's iphone apps as well... e.g. Go to iPhone Settings -> Mail, Contacts, Calendars -> Signature.
EDIT: since you cant use the standard way you can move the complete view up with following code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint currentCenter = [self.view center];
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self.view setCenter:newCenter];
[UIView commitAnimations];
return YES;
}
change the animation duration and the change in position of the center (150) in this code according to your requirements. I have assumed that your view controller is the delegate for your textfield.
To bring the view back to it's original position, use same code but add to center.y in textFieldShouldReturn or textFieldShouldEndEditing etc
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
P.S. : I'm still not sure you should be using this second approach.