UITableView header height changes, but the tableview does not move - objective-c

In my project, the UITableView header shrinks by about 35 pixels due to the hiding of a UI element.
I can change the UITableView header and all related constraints; but the table view itself never seems to move and stays at the offset of 35 pixels leaving a gap.
Is there something I need to be doing to ensure that the UITableView is always snagging or hugging the bottom of the UITableView header; regardless of what size it might be?
Many thanks

Figured it out, I had to recalculate the new height and set the height of the table view header.
CGRect newFrame = self.tableView.tableHeaderView.frame;
newFrame.size.height = newFrame.size.height - (height-10);
self.tableHeaderView.frame = newFrame;
[self.tableView setTableHeaderView:self.tableHeaderView];

Related

UIVIew from XIB with Autolayout to UItableView Header

I am writing because I have a problem with the Auto Layout.
I'm trying to create a simple view in InterfaceBuilder with Auto Layout I want to load code and enter as a header of a table (not as header section). I explain briefly what are the characteristics.
The imageView must be square and must be as wide as the screen.
The space under the picture to the bottom of view that contains the button and label must be high 50 points.
Between image and button has to be a fixed distance of 12 points.
Between image and label must be a fixed distance of 13 points.
All these features are able to get them with Auto Layout. I added a constraint to the aspect ratio of the image (1: 1) and the various constraints for distances. all right.
The real problem is that by launching the app on iphone 6+ simulator (414 points of width), the image (with the label and button) goes above the cells.
Enabling various transparencies I noticed that the superView of Image View, only increase the width. It does not increase its height! How do I fix?
This is the code:
- (void)viewDidLoad{
//...
PhotoDetailsHeaderView *hView = (PhotoDetailsHeaderView *)[[[NSBundle mainBundle] loadNibNamed:#"PhotoDetailsHeaderView" owner:self options:nil] objectAtIndex:0];
hView.delegate = self;
self.tableView.tableHeaderView = hView;
//...
}
This is how I create the xib:
and this is how it is on the simulator, the green box is Uiimageview and the yellow box (under green box) is the mainview (or superview):
How can fix it?
Many thanks to all!
You'll need to add a property to store your PhotoDetailsHeaderView:
#property (nonatomic, strong) PhotoDetailsHeaderView *headerView;
Then calculate its expected frame in viewDidLayoutSubviews. If it needs updating, update its frame and re-set the tableHeaderView property. This last step will force the tableView to adapt to the header's updated frame.
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
CGRect expectedFrame = CGRectMake(0.0,0.0,self.tableview.size.width,self.tableView.size.width + 50.0);
if (!CGRectEqualToRect(self.headerView.frame, expectedFrame)) {
self.headerView.frame = expectedFrame;
self.tableView.tableHeaderView = self.headerView;
}
}
The problem is probably that in iOS you have to reset the header of the table view manually (if it has changed its size). Try something along these lines:
CGRect newFrame = imageView.frame;
imageView.size.height = imageView.size.width;
imageView.frame = newFrame;
[self.tableView setTableHeaderView:imageView];
This code should be in -(void)viewDidLayoutSubviews method of your view controller.

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.

Can't move my UIScrollView

I have a content size 1000x10000 and it's center is on the center of the viewcontroller.
I want to push a button for it to turn pages. In the next code I've tried almost any number in the origin.x but nothing changes the scroll view.
I don't understand the math of it but when I set origin.x=300 and origin.y=100 the view will move up a bit but that's it.
CGRect frame=scroller.frame;
frame.origin.x=ANY NUMBER HERE;
frame.origin.y=0;
[scroller scrollRectToVisible:frame animated:YES];
How can I set it to move the view from right to left
Try this,
[scroller setContentOffset:CGPointMake(300, 0) animated:YES];
setContentOffset:animated:
Sets the offset from the content view’s origin that corresponds to the receiver’s origin.
You should be creating the UIScrollView's frame to the size of the UIViewController like so:
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
Then set the scrollers CONTENT VIEW to the 1000x10000 size like so:
scroller.contentSize = CGSizeMake(1000, 10000);
The contentSize is what makes a UIScrollview able to scroll, as long as the contentsize is larger than the scrollers frame.
If you want to make it scrollable, then contentSize has to be greater than frame.size, otherwise there isn't space to scroll.
Then ensure that scrollEnabled is set to YES (by default it is, so unless that you've changed it it's already set to YES).

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.

UILabel text overrun in custom tableCell

I have a weird problem in a custom UITableCell subClass.This custom cell has a pair of UILables that are multi-line and can contain varying amounts of text (up to 2000 characters). The custom tableCell has a layout method where I am calculating their frame's height using the code below, taking the device orientation into account:
if (isPortrait) {
presentationTextLabelSize = [presentationTextStr sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(portraitDescriptionWidth, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
presentationTextLabelRect = CGRectMake(60.0f, 25.0f, portraitDescriptionWidth, presentationTextLabelSize.height);
} else {
presentationTextLabelSize = [presentationTextStr sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(landscapeDescriptionWidth, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];
presentationTextLabelRect = CGRectMake(60.0f, 25.0f, landscapeDescriptionWidth, presentationTextLabelSize.height);
}
self.presentationTextLabel.frame = presentationTextLabelRect;
I have the label's autoResizeMask set as follows:
self.presentationTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
I use the same calculation in the UITableViewController to set the row height.
Everything works perfectly, the label is sized to the correct height for it's width, and the cell autoResizes on device rotation, except that when the device is rotated to landscape the label's text sometimes extends out of the label's frame to the right, clear past the edge of the tableView. If I scroll the cell out of view and back again it's back to normal. I am logging the cell's width to the console, and it is correct, yet the text extends far past that width.
Any ideas what's joing on?
Thanks
The problem is, the tableView is not reloaded when the device is rotated to landscape. So, there is mismatch in updating the texts. Use [tableView reloadData]; at the place, where you find the device is changed to landscape mode.