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;
Related
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:
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;
}];
}];
I have a button which when pressed, should push the controls below it down and show textFields on the space freed. Basically, just changing the position of these controls in an animated way. Any ideas/tips on how to do that?
I tried the code in: How to Make a UITextField Move Up When Keyboard Is Present
But couldn't get it to work just changing the methods. Appreciate any useful information.
Thanks!
I think you have write code for that. If your controls below is a view (BottomView), when you press the button, change the frame of the BottomView, to make room for the textfield view. Now add the textField there. Write the code in a Animation block.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// bottomView.frame = (new frame)
// design a textfield in IB. make outlet.
// textField.frame = (new frame)
[UIView commitAnimations];
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.
Is there a way to make a keyboard disappear without resignFirstResponder? I have a UITextField, and I'd like it to be able to accept new text and still have the insertion point (flashing cursor) visible without the keyboard being on screen.
When I perform a [textField resignFirstResponder] I can still insert text, by appending to the textField.text, but I can't see the insertion point anymore.
Is there a standard way to make the keyboard animate out, while having my textField remain first responder?
Thanks.
Found an answer to this question. It's not standard though, and like Dave says, may be taboo for the app reviewers. Using the code from this page as a starting point:
http://unsolicitedfeedback.com/2009/02/06/how-to-customize-uikeyboard-by-adding-subviews/
I added in an animation block. You can dismiss the keyboards view with a standard looking animation, but whatever is first responder will retain that status. Really, the keyboard is just hidden off screen.
- (void)hideKeyboard
{
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
// Now iterating over each subview of the available windows
for (UIView *keyboard in [keyboardWindow subviews]) {
// Check to see if the view we have referenced is UIKeyboard.
// If so then we found the keyboard view that we were looking for.
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// animate the keyboard moving
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.4];
// remove the keyboard
CGRect frame = keyboard.frame;
// if (keyboard.frame.origin.x >= 0) {
if (keyboard.frame.origin.y < 480) {
// slide the keyboard onscreen
//frame.origin.x = (keyboard.frame.origin.x - 320);
// slide the keyboard onscreen
frame.origin.y = (keyboard.frame.origin.y + 264);
keyboard.frame = frame;
}
else {
// slide the keyboard off to the side
//frame.origin.x = (keyboard.frame.origin.x + 320);
// slide the keyboard off
frame.origin.y = (keyboard.frame.origin.y - 264);
keyboard.frame = frame;
}
[UIView commitAnimations];
}
}
}
}
I wrote in code to dismiss the keyboard to the left and to the bottom of the screen. I don't know what will happen when you eventually resignFirstResponder, but it might be an idea to reset the keyboard frame when you do that.
If there is, then it's not documented in the iPhone API. Also, what you're asking for does not make sense. You want to have the insertion point in a UITextField. OK, great. But you also want the keyboard to go away? Then what's the point of the textfield having the focus? How are you going to input data into the textfield? If you want to have a custom keyboard, then just display it on top of the keyboard. I can't think of a good reason why you'd want the cursor to be visible but not have some sort of data entry mechanism. That would break UI convention and might even get your app rejected.