Setting UITableView ContentInset - objective-c

I am changing content insets bottom value manually.
literatureTableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
This works fine but i want to set the value depending on the height of the table contents. I tried some things with tableView.bounds.size.height etc. but nothing is working.

If you need to know the calculated bottom inset prior to displaying the reloaded data, then you must calculate the heights manually.
However, if your app permits you to call reloadData and then adjust the inset, you can arrive at it much more simply by doing this:
CGFloat trueContentHeight;
CGFloat heightWithInsets = self.tableView.contentSize.height;
UIEdgeInsets insets = self.tableView.contentInset;
trueContentHeight = heightWithInsets - (insets.top + insets.bottom);
Then you can calculate the bottom inset with the trueContentHeight and the height of the tableView's bounds.

youll need to add up the height of your cells and headers (if you have any). You can use
tableView:heightForRowAtIndexPath: from self to get the height's manually and add them up if they are not all the same.

Related

iPhone MKMapView: set span/region value to show all pins on map

I'm working on a project (platform iOS 7) in which i required current location with stores around 5km, so how to calculate the span/region value to display all stores with current location on map.
MKMapRect zoomRect = MKMapRectNull;
double inset;
for (id <MKAnnotation> annotation in mapVW.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
inset = -zoomRect.size.width * 20;
}
[mapVW setVisibleMapRect:MKMapRectInset(zoomRect, inset, inset) animated:YES];
this is what I'm trying
Thanks
It's not clear what your exact issue is but the following may help:
The calculation of the inset looks wrong. It's setting the inset (padding on the sides) to 20 times the width of the whole zoom area. What you probably want is to set the inset to a small fraction of the entire width. Maybe you meant 0.20 instead of 20.0:
inset = -zoomRect.size.width * 0.20;
You also don't need to repeatedly set the inset inside the for loop since it only depends on the final width. You can move the above line after the for loop before calling setVisibleMapRect.
You mention some issue with the current location. It's not clear what the issue is but maybe you mean that this zooming code doesn't include the current location? If so, maybe the current location hasn't been determined yet when this code is called. Try moving this code to (or also call it from) the didUpdateUserLocation delegate method. Make sure showsUserLocation is YES and that the map view's delegate is set.
By the way: iOS 7 includes the new method showAnnotations:animated: which automatically determines the bounding rectangle for some given annotations and sets the map's visible region for you. It doesn't let you specify a custom inset like you are doing (though the default isn't bad). So instead of the above loop, you would do:
[mapVW showAnnotations:mapVW.annotations animated:YES];
NSArray *anno_Arrr = mapview.annotations;
[mapview showAnnotations:anno_Arrr animated:YES];

How to get the padding from the edge of the UITableview to the UITableViewCell

On the iPad, the Grouped style tableview's cells are inset deeper from the edge of the tableview than on the iPhone.
I need to retrieve the Left and Right distances from the edges of the tableview to where the cell begins. What i'm referring to is similar to "Margins". I read the UITableview API up and down and can't find a property that returns this.
I need to use this in calculation to compute where to position content in my cells.
Thanks in advance!
Alex
I haven't tested this but i'm pretty sure you should just be able to pick up the frame of both and then compare from there.
CGRect cellFrame = yourCell.frame;
CGRect tableFrame = yourUITableView.frame;
The CGRect values are (x coordinate, y coordinate, width, height).
Also you can just print out the frames using :
NSLog(#"your cell frame is %#",NSStringFromCGRect(yourCell.frame);
NSLog(#"your table frame is %#",NSStringFromCGRect(yourUITableView.frame);
I solved this with overriding the layoutSubviews call for the iPad and setting the grouped view margins to what I want them to be, rather then what the apparently hidden value is. Other answers in Stack point out that it can vary from 10 to 45 pixels in width.
In your CustomTableViewCell class
- (void)layoutSubviews
{
CGRect f = self.bounds;
f = CGRectInset(f, 10, 0);
self.contentView.frame = f;
self.backgroundView.frame = f;
}
You could force it to keep contentView and backgroundView to be equal to that of the TableCell width which is that TableView width, but in this case I still wanted my grouped view to be inset a little bit. It also allows you to better match with a custom header/footer view which will go edge to edge without work.

Maintain scroll position in UIWebView when resizing

I have a UIWebView which resizes when the device is rotated. When the webview is resized the scroll position from the top of the page remains the same, but since the height of the content is changing, you end up in a different place at the end of the rotation.
The content of the webview is text and images which are not being scaled to fit. At a smaller width the text breaks more making it taller.
Is there a good way to maintain the scroll position?
One way to kind of work around this problem is to adjust the contentOffset of the webviews's scrollview after a rotation. Due to changes in line breaks in texts the overall size of the page may change during a rotation.
So to solve this, you have to save the old size of the webviews content in the "willRotateToInterfaceOrientation"-method, then calculate the relative change in the "didRotateFromInterfaceOrientation"-method and adjust the scrollviews contentOffset. (That 'almost' solve the problem - i say almost because due to the changes in line breaks, you might end up one or two lines off your desired position.)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
oldSize = self.webView.scrollView.contentSize;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGSize newSize = self.webView.scrollView.contentSize;
float xFactor = newSize.width / oldSize.width;
float yFactor = newSize.height / oldSize.height;
CGPoint newOffset = webView.scrollView.contentOffset;
newOffset.x *= xFactor;
newOffset.y *= yFactor;
webView.scrollView.contentOffset = newOffset;
}
Take a look at my solution: https://github.com/cxa/WebViewContentPositioner. In short, I use document.caretRangeFromPoint(0, 0) to capture the Range, and insert an element to track its position. After resizing, use the captured Range info to decide position.

UIScrollView: handling of dynamic content?

I've a Storyboard with a UIScrollView which contains two UILabels, a UIImageView and a UITextView. The content of the UIImageView and UITextView is dynamic and so are their height.
Currently I'm doing this inside my viewDidLoad to adjust the size of the UITextView after the dynamic text is inserted:
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Is this the way to change its height?
My next problem is to set the content size for the UIScrollView, to activate the scrolling. Is there a smart way to get the height of all its content or do I have to calculate the height for each element and set the sum of this as the content size of the UIScrollView?
IF you had no space in between your objects, you could make a for loop in your scrollView.subviews and add up all the heights to set as the contentSize.
As you probably don't have everything tight together, you're probably better by getting the bottom most object and adding up it's frame.origin.y and it's frame.size.height (maybe you want to have some extra space in here, but that's up to you) and that will give you your contentSize.height to keep everything in there.

Expand a UITextView when clicked?

How would I go about animating open a textviewto expand when clicked? So if I have a UITextView that is of height 30 but when it gains focus have it expand to 100. Then on losing focus have it go back to 30 even if the text is of height 100.
TIA
Look into the UITextViewDelegate methods -textViewDidBeginEditing: and -textViewDidEndEditing:
Interesting you ask this, because I literally just figured out how to do the same thing. Call this method whenever you want to resize your textview:
-(IBAction)resizeFrame {
textView.frame = CGRectMake(x, y, width, height);
[self.view bringSubviewToFront:questions];
}
Line 1 sets the frame of the textView as a rectangle starting at point x,y with whatever width and height you specify.
Line 2 brings this view to the front so that you can actually see it!
If you want to size it back down, call another method with a different name that sets the CGRect back to the textView's original size.