Making UITableView scroll to right location when using custom keyboard - iphone

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!

Related

How to tune the onFocus auto scroll distance?

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

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!

scale a UITableView to fullscreen from inside a UIScrollView

Having failed in trying to puzzle together different sources to any form of coherent approach or concept I turn, once again to the learned people of StackOverflow. My problem is quite specific and maybe that's why I'm having trouble finding information that fits or maybe I just suck at searching. Either way, here goes.
I am building an app which has a UIScrollView populated with UIViews which in turn are populated with UITableViews. I have paging and everything set up and working properly. Basically, I am trying to mirror the functionality of Safari's (mobile) tab behaviour, or (even closer to what I'm trying to achieve) Tweetdeck's main page. I couldn't post an image but if you click on the link below you'll see what I mean.
http://www.redmondpie.com/wp-content/uploads/2009/07/TweetdeckIphone04.jpg
The reason for the UITableViews are inside UIViews is that this was the only way I could figure out to make the tableViews have a space between them, instead of being right next to each other. I tried setting the size of the tableViews and the inset of the scrollView, among many things but the tableviews always ended up filling the entire scrollView. In any case, that is a separate issue, possibly
As I click on a tableView/UIView inside the scrollView I want that tableView/UIView to scale up and fill the entire screen (minus tabBar and navigationBar), and then I should be able to scale it back down by pressing the back button on the navigationBar.
Any information or nudges in the right direction is greatly appreciated.
I am doing something similar in my app:
-(void)displayPage:(int)targetedPage {
...
[UIView beginAnimations:#"MultiViewAnimate" context:nil];
[UIView setAnimationDuration:0.3];
//animate the frame
//targetedPage is the index (table view) that should be displayed
[(UITableView *)[tableViews objectAtIndex:targetedPage] setFrame:CGRectMake(0, 0, 320, 372)];
[(UITableView *)[tableViews objectAtIndex:targetedPage] setUserInteractionEnabled:YES];
...
[UIView commitAnimations];
}
-(void)displayMultiView:(id)sender {
...
[UIView beginAnimations:#"MultiViewAnimate" context:nil];
[UIView setAnimationDuration:0.3];
//animate the frame
//targetedPage is the index (table view) that I was previously looking at full screen
[(UITableView *)[tableViews objectAtIndex:targetedPage] setFrame:CGRectMake(60, 70, 200, 232)];
[(UITableView *)[tableViews objectAtIndex:targetedPage] setUserInteractionEnabled:NO];
...
[UIView commitAnimations];
}
In this code tableViews is an array of all the UITableViews that the scrollView contains.
I am doing a little more than this because I am taking a screenshot of my view and using a UIImageView for the scaled view and then animating back out to the respective UIWebView. One advantage to that approach is that I can just attach a UITapGestureRecognizer to the UIImageView and not have to mess with turning off userInteraction on the UITableView.

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