How to tune the onFocus auto scroll distance? - iphone

I am trying to tune the auto scroll distance of the default IOS behaviour.
As shown in the picture, by default, the screen will scroll automatically up so that the key board won't hide the section you are editing.
But the problem with this is that it only scrolls up high enough so that the top of the key board overlapping on the bottom of the cell.
What I want is something like this:
So instead of just moving up till the keyboard won't cover the textField, it should move up some extra space so that the next textField could be seen as well.
Ok, now I finish the description. Here is what I have tried.
1. I have tried to delegate the didSelectRowAtIndexPath to roll up the table view.
2. I have tried to use a tap gesture recognizer to handle the tapPosition and then scroll.
These two methods actually works to some extent but they are still not 100% what I want. Because once I tap focus on text field the table view still not moved high enough so that I can see the next field. (It seems to me that the textFields focus on event has higher priority than the tap gesture and cell selection)
Now this is what I want, I was wondering if there is a way to tune the default scroll up behaviour on focus of the text field.
Thanks.

Try this,
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[self.scrollView setContentOffset:CGPointMake(0.0, self.activeTextField.frame.origin.y-92) animated:YES]; // change the value "92" as per your scroll height.
[UIView commitAnimations];
}

Related

UITableView frame change animation issue

I've googled about this problem a lot but there seems to be no answer. So I'm hoping some of you may know how to deal with this. I have a view controller that has a tableview, when I change the view frame with animation, everything goes well, except one particular case, when tableview has more items than it can fit to screen, and only when the tableview is scrolled to bottom. Then if I shrink the view height, my view animates correctly, but the tableview somehow jumps up a bit and only then animates to the bottom.
If I shrink the view, but tableview isn't scrolled to bottom (even if I can see the last cell, lets say a bit more than half of it) it does animate correctly.
I've tried couple of things, like setting autoresizing masks on and off and also animate from current state or something like that, but that didn't help :/
So any suggestions what could be the problem?
EDIT:
Code that i use to change frame
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[_contView setFrame:CGRectMake(0, 0, 320, 420)];
}
completion:^(BOOL finished){
}];
I fought this same bug for awhile before realizing I couldn't figure out for the life of me why it was happening...until I found a very detailed explanation and solution.
Full write-up can be found here:
http://lists.apple.com/archives/cocoa-dev/2012/Apr/msg00341.html
Quick solution is to change contentInset instead of the table view frame. Just add the height of whatever you are showing (ie a keyboard or ad banner view) to the contentInset.bottom property. The scrolling area will be increased to account for the required resize.
In this example, I was showing an ad banner view on top of my tableview:
UIEdgeInsets insets = myTable.contentInset;
insets.bottom += 50; // 50px is the height of the ad banner view
myTable.contentInset = insets;
I used to have a similar problem and I implemented this workaround (Disclaimer: this is a hack to a currently-unresolved iOS bug):
// check if the table view is scrolled to the bottom
if (tableView.contentOffset.y + tableView.frame.size.height == tableView.contentSize.height) {
// if it is, shift the table view contents up one pixel
[tableView setContentOffset:CGPointMake(tableView.contentOffset.x, tableView.contentOffset.y - 1) animated:NO];
}
// call the animation block afterwards here
It's a hack, though not noticeable to the user since it's just a 1 pixel motion. UITableView has a few bugs (reported to Apple already, but not yet resolved) when it comes to animations from a scrolled-to-bottom position.
You should use autoresizing from xib which is right corner of xcode ... I think this may help you.
Try to add footer view to your table with non-zero height.

Label and button don't go in background

In my iPhone project, I have a mapview. I have one custom button and a label on map view. When user clicks on a pin a callOutAnnotation is called. I have a different class for CallOutAnnotation. A view for callOutAnnotation appears on the map. When CallOutAnnotation view appears on the map, the custom Button and label appear over the CallOutAnnotation view. I have tried a lot more things but they don't go to back of CallOutAnnotation view. need help.
Thanks and regards,
PC
If I understood it correctly, you've placed a label and a button over your map, which is pretty much the problem you're having.
What happens now is that any subviews added to your mapView are automatically below your label and a button. You could try adding them as subviews to your map (I'm not sure if this will work), but probably the best way to go around this is to place the button and the label somewhere less out of the way.
You could always try changing the alpha of the button and the label to 0 when you want them to be hidden then back to 1 when they should reappear.
button1.alpha = 0;
You can even animate this if you want to:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
//enter code
[UIView commitAnimations];
Hope it helps!

How to make the textfield move up when keyboard covers it up

I have an application in which I have 2 textfields and a textview. When I click on the first textfield my keyboard popsup and theirs is no problem but when I type in the second textfield my keyboard popsup and covers the textfield.
I want that when I click on the second text field, the textfield should move up little bit so that I can type in and I have a textview. But I have written code for textview so that when I type in textview it automatically moves.
The problem is with textfield. How can I solve this problem?
Consider using a UITableViewController. Otherwise implement UITextFieldDelegate and move your UIView to the desired position in the - (void)textFieldDidBeginEditing:(UITextField *)textField method.
Check out the link below - the solution is written by Micheal Tyson. It addresses UITableView and UIScrollView, can be easily changed and works just as a drop-in component. I'm using it and it works well.
A drop-in universal solution for moving text fields out of the way of the keyboard
Create two methods like given below , first one is for bringing the view slightly upwards, and second one is to bring the view to its original position back
First method:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -175);
[UIView commitAnimations];
Second method:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, 175);
[UIView commitAnimations];
[self.destext resignFirstResponder];
Call these methods on textfieldEditingDidbegin and DidEndonExit
Add the view to a UIScrollView. Then use the UITextFieldDelegate methods to set the contentOffset of the scrollView when textField is tapped. Reset the contentOffset when the user has finished entering text.
I have created a simple subclass of UIView containing UITextView and a send button that moves up when keyboard shows and moves down when keyboard hides. In addition to that, UITextView resizes according to the amount of text in it.
Have a look at here:
https://github.com/kerrygrover/KBTextView
An obvious one that you've probably tried already, but one that took me a while to latch onto, is to change from landscape to portrait.

Making UITableView scroll to right location when using custom keyboard

I have a UITableView and I need to use a custom keyboard with it and, for a few reasons, I can't use it as an inputView. So I am having a my keyboard view appear as a subview. Its height is 260. I want the table view to scroll so that the selected cell always has a y-position between 0 and 260. Is this possible? Here is what I am currently trying... (this is in the keyboard class)
-(void)showForCellAtIndexPath:(NSIndexPath*)indexPath{
[UIView beginAnimations:nil context:NULL];
self.view.frame = CGRectMake(0.0,220.0,320.0,260.0);
delegate.tableView.contentInset = UIEdgeInsetsMake(0.0,0.0,260.0,0.0);
delegate.tableView.scrollIndicatorInsets = delegate.tableView.contentInset;
delegate.view.frame = CGRectMake(0.0,-210.0, delegate.view.frame.size.width,delegate.view.frame.size.height);
[UIView commitAnimations];
[delegate.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
I would love your help. Thanks!
Alright...this solution is kind of a hack, but if someone searches and finds this, I don't want them to go thru the hell I did to figure it out.
What I did was modify the header height of the first section of the grouped table view to be large (300px was big enough for me). Then I changed the y-inset (in my case to -255), so that everything looked normal. When the user selects a section, I immediately eliminate the inset and do a scroll to bottom... Then, when the user hits done, I re-add the inset, so that the user doesn't have to go through extra scrolling.
I hope this helps anyone who has this same issue!

Scrolling screen upward to expose TextView above keyboard

I think I'm missing something obvious and would appreciate an answer.
I have a view with a 2-section grouped tableView, each section having one row and a textView, the heights of the rows 335 and 140. This allows for a box with nicely rounded corners to type text into when the keyboard appears (140 height section) and when the keyboard is dismissed, a nice box to read more text (notes); most of the time, use is without the keyboard.
I also added a toolbar at the bottom of the screen to scroll up above the keyboard. A button on the toolbar dismisses the keyboard. This last part works fine with the keyboard going up and down using a notification and the following code in a keyboardWillShow method:
[UIView beginAnimations:#"showKeyboardAnimation" context:nil];
[UIView setAnimationDuration:0.50];
self.view.frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 216);
[UIView commitAnimations];
But with the above code, the 2 sections of the tableView remain unscrolled, only the toolbar and the keyboard move. With the following code (found both in previous posts), both the toolbar and the tableView sections move.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.50];
CGRect rect = self.view.frame;
rect.origin.y -= 216;
self.view.frame = rect;
[UIView commitAnimations];
Now I know that I have to tweak the numbers to get the everything as I want it but my first question is what is substantively different between the 2 sets of code that the sections move in the 2nd but not in the 1st? The toolbar also moves with the 2nd code.
The second question is, am I going to be able to scroll the smaller height section from off the screen to above the keyboard while at the same time moving the toolbar up just 216?
Thanks
I may be missing something here, but in the first piece of code, you are changing the height by 216. While on the 2nd piece of code, you are changing the origin of the entire view by 216 in the y-direction.
Depending on how you have the frame set in IB, the first piece of code might not move it if you don't allow it to move in that direction. Check your settings in the inspector window.
When you are saying "unscrolled" you are referring to them not changing location within the main view correct? This would be different than them actually scrolling (which you can do as well by changing scroll value).
You may want to check out the Hidden Drawer example here, since they push views in and out of the main view, sort of like a toolbar, and I think that is what you are asking. The whole "scrolling" thing is throwing me off though.
http://cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html
From memory, something like
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight