keyboard hiding textfields in tableViewController - iphone

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.

Related

Setting UIControl's frame changes view bounds but not where it can receive touches

I have a headerView which can expand and collapse when pressed. It's default state is collapsed.
I use the following code to expand the headerView.
if (!self.headerViewIsExpanded) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self.collapsedHeaderView removeFromSuperview];
self.headerView.frame = self.expandedHeaderView.frame;
[self.headerView addSubview:self.expandedHeaderView];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y + offset, self.tableView.frame.size.width, self.tableView.frame.size.height - offset);
[UIView commitAnimations];
self.headerViewIsExpanded = YES;
}
It correctly expands the view frame and diminishes the tableView frame. However, it fails to expand the touches received area of the original UIControl, so I can only collapse it by pressing where the original frame was.
How can I resolve this issue?
EDIT: Alternatively, I would accept an answer with a good explanation of why I cannot resolve the issue, or at least not resolve it safely.
EDIT 1: To be more clear, the headerView is NOT a tableView header view. It is just a view that sits on top of the table that happens to be called headerView. Both headerView and tableView are subviews of the main UIView that spans the available window.
You can try to overload layoutSubviews method. Inside you should use setFrame method for your headerView to define all view elements position.

Page control directly switching

I have scroll view which contains 5 views and it include page control when i clicked over fourth bubble of page control it moves to second view but i want it should directly move to fourth view please help me to solve this problem.
calling of pageControl is as follows:
[pageControl addTarget:self action:#selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
implementation of pageTurn method is as follows:
- (void) pageTurn: (UIPageControl *) aPageControl
{
int whichPage = aPageControl.currentPage;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
scrollView.contentOffset = CGPointMake(320 * whichPage, 0);
[UIView commitAnimations];
}
and my scrollViewDidScroll is as follows:
- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
CGPoint offset = aScrollView.contentOffset;
pageControl.currentPage = offset.x /320 ;
}
If you look at the UIPageControl in the documentation, it says this:
When a user taps a page control to move to the next or previous page, the control sends the UIControlEventValueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.
The UIPageControl can only be used to switch one page at a time, hence it only changes to the second page when you click it (and not the fourth, no matter what dot you press).
You'll probably want to create custom UIButtons that make up your pagination control.

Raise View when keyboard visible in landscape

I have a problem when in landscape rotation on my App. Everything moves to where I want it etc, but when the keyboard is visible, one of the text fields is covered. I have written some code that pushes the view up to resolve this. This bit of code works fine in portrait, but in landscape, the view rotates back to portrait. The code is below:
- (void)textFieldDidBeginEditing: (UITextField *)textField {
CGAffineTransform translation = CGAffineTransformIdentity;
if (textField == self.incVATField)
translation = CGAffineTransformMakeTranslation(0, -30);
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
}
Can anyone tell me how to resolve this, so that when the device in lanscape, the view just gets pushed up a little so I can still see the text field?
Thanks
When the editing of the text field begins, just adjust the frame of the view so that u can see the text.
- (void)textFieldDidBeginEditing: (UITextField *)textField {
self.view.frame = CGRectMake(x_origin,y_origin,width,height);
}
Another option:
You can also do this by making your view a sub view of scroll view and changing the content offset to the required point so that the text field is visible.

How to slide(push) controls down/up in a View?

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];

How can I make the keyboard disappear?

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.