I would like to dynamically resize UICollectionView height and display all cells without scrolling when page loads.
I tried following codes in viewDidLoad but didn't work
self.constraintTableViewHeight.constant = self.myProfileCollectionView.contentSize.height
or
self.constraintTableViewHeight.constant = self.myProfileCollectionView.collectionViewLayout.collectionViewContentSize().height
call
self.view.layoutIfNeeded()
after
self.constraintTableViewHeight.constant = self.myProfileCollectionView.contentSize.height
If it will not help try to move this code to view viewWillAppear method.
Related
I have a TableView footerView with two labels that can hold multiple lines.
I'm using this code in viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
label1.preferredMaxLayoutWidth = tableView.frame.size.width - Constant.footerMarginSpace
label2.preferredMaxLayoutWidth = tableView.frame.size.width - Constant.footerMarginSpace
footerView.setNeedsLayout()
footerView.layoutIfNeeded()
footerView.frame.size.height = footerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
tableView.tableFooterView = footerView
}
This is working fine (the labels will eventually spread over multiples lines and the size of the view is adjusting) when the labels value are set before the view is shown, but If I update their content after, the view won't update its layout, even when using tableView.reloadData().
If I push another view on top of my view, and dismiss it, the layout will be correct.
What is the proper way to update the layout and size of my TableView footer ?
I think it might be easier to calculate how many lines you need based on your data and return the correct height using
tableView(_:heightForFooterInSection:)
This method will be called to layout your tableview when the reloadData is called.
I am trying to embed a table view into a scroll view. I would like the scrolling to happen on the scroll view level. I am able to embed the table view into the scroll view but I am stuck at trying to resize the scroll view content size to fit the full height of the table view..how can I achieve this?
you can calculate tableview height in heightForFooterInSection or viewForFooterInSection of table
_tblOption.scrollEnabled = NO;
Frame = _tblOption.frame;
Frame.size.height = _tblOption.contentSize.height;
_tblOption.frame = Frame;
_mainScrollView.contentSize = CGSizeMake(_mainScrollView.contentSize.width, _tblOption.frame.origin.y + _tblOption.contentSize.height);
Your hierarchy should be as follow: UIView -> UIScrollView -> UITableView. That mean you simple create UIViewController, then place UIScrollView, connect delegates and so forth, then place in it UITableView controller.
But im wonder why would you put tableView inside scroll view, better design would be place separately UITableView and UIScrollView.
I much rather recommend you to have UIView with UITableView AND UIScrollView, placed separately.
In XCode you may use Editor->Embed In.
I have two questions related to UITableViews.
1) The first one is, what is the gap at the top of the UITableView? My app always starts with the top cell not flush with the top of the tableview (as shown in the second image), it starts about one cell lower, i.e. where that gap is in the interface builder. I can't find where that is coming from, or why.
2) I can't seem to resize the uitableview programmatically, I'm trying to reduce the height after a popup appears. I've shown an example of it not working in the second picture.
Here is (an example of) what I am trying at the moment:
- (void)viewDidLoad
{
[super viewDidLoad];
self.table_view.delegate = self;
CGRect tableBounds = self.table_view.bounds;
tableBounds.size.height -= 100;
self.table_view.bounds = tableBounds;
CGRect tableFrame = self.table_view.frame;
tableBounds.size.height -= 100;
self.table_view.frame = tableFrame;
}
Thanks!
UITableView Selected:
Simulation:
In your xib (or storyboard) put your UITableView at position (0,0). ( the same position as the navigation bar).
The first image shows that your table view has problems even in Interface Builder. It looks as if you've set the top inset incorrectly; check your edge insets.
The reason your resizing code is not working is probably that it is too early (viewDidLoad); put it in viewDidAppear: and see if that works, and if it does, try moving it back to viewWillAppear: so the user doesn't see the resizing. If it doesn't work, it might be because you're using auto layout; you can't manually alter the frame of something whose frame is dictated by auto layout. (Your resizing code is also silly; you want to set the frame, not the bounds.) But it might also be because you're using a UITableViewController in a UINavigationController; if you do that, the table view is under the navigation controller's direct control and its size is not up to you.
I got my collectionview of photos to change layout with the following code:
newLayout = [[SDCGalleryLayout alloc] init];
and then
[self.collectionView setCollectionViewLayout:newLayout animated:YES];
It works fine and it animates from one layout to another. However, the view inside my collectionviewcell is not resizing to account for its new cell size. drawRect() of the views inside the collectionviewcell is not being called.
Any suggestions to get the views inside my collection cells to redraw after changing a layout?
Thanks
use -(void)layoutSubviews of the UIView inside the collection view cell. Works like a charm
I am using a UICollectionView with fixed size CollectionViewCells. I display this CollectionView in a popOver with an UIPopoverController.
My goal is that the popOver automatically resize to the CollectionView's content. So if I only have 4 cells, than it should be small. And if I have more cells, it should be bigger. I want to avoid the empty space left in the popOver.
So I don't want a fixed popOver size, but a variable one depending on the number of cells in the CollectionView.
Any suggestions?
I had a hard time figuring this out myself but just got it finally. You have to access your Collection View's collectionViewLayout property and then use the collectionViewContentSize method to get the size of your content. You can then use those to resize your Collection View by setting it's frame.
CGRect rectangle;
rectangle = _collectionView.frame;
rectangle.size = [_collectionView.collectionViewLayout collectionViewContentSize];
_collectionView.frame = rectangle;
Now that your collection view is sized to it's content, you can set the popOver's size based on the collection view size. It may look something like this.
popOver.frame = _collectionView.frame;
or
[popOver sizeToFit];