3

I am trying to open a new view on select of a table cell in a previous view. The new view that I am trying to open, consists of different sub-views or modules. Hence, I populate each sub-view one by one inside in [self populate] method which is in triggered inside the viewDidLoad method.

-(void)viewDidLoad{
  [super viewDidLoad];
  [self populate];
}
-(void) populate{
    [self.edgeGallery loadImagesWithURLs: _items];

    // Modular view: main info
    [self.vwListingMainView setListing: _listing];
    [self.vwListingMainView refresh];

    // Modular view: listing agents
    _vwListingAgentsView.agentsArray = _listing.agents;

    // Modular view: listing info
    _vwListingInfoView.listing = _listing;
    [_vwListingInfoView refresh];

    // Modular view: Listing activities
    _vwListingActivityView.listing = _listing;
    [_vwListingActivityView requestCounts];

}

Every time a new subview is populated, the method viewWillLayoutSubViews is called. This is the method where I compute the subview's height and other constraints and append it to the superview.

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self computeAndFixHeight];
}
- (void) computeAndFixHeight {
    // Adjusting each module's height
    _cstMainInfoHeight.constant = [_vwListingMainView getViewHeight];
    _cstListingActionsHeight.constant = [_vwListingActionsView getViewHeight];
    _cstListingAgentsHeight.constant = [_vwListingAgentsView getViewHeight];
    _cstListingInfoViewHeight.constant = [_vwListingInfoView getViewHeight];

    // Adjusting scroll view height
    NSInteger computedScrollHeight = _vwListingUpcomingEventView.frame.origin.y + [_vwListingUpcomingEventView getViewHeight];

    [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, computedScrollHeight)];
    _cstContainerBottom.constant = -computedScrollHeight - kDefaultNegativeScrollH;

    [self.view layoutIfNeeded];
    [self.view updateConstraints];

}

However, once the view is loaded completely, the problem that I am facing is that sometimes, I get the complete view and sometimes, randomly, I get an empty view. I think [self.view layoutIfNeeded] is the problem, but I have also tried using [self.view setNeedsLayout] and [self.view setNeedsUpdateConstraints], but still the problem remains. Any help would be appreciated.

Please excuse me if I am doing anything stupid. I am new to iOS development.

Ron
  • 389
  • 1
  • 4
  • 16
  • First of all, you need to call super in viewDidLoad(): [super viewDidLoad]; – onnoweb Oct 26 '16 at 22:38
  • I think you're not using a UITableView. It seems you're just using a UIScrollView, right? In that case, if you set your constraints right, then it shouldn't be necessary to implment `computeAndFixHeight`. Check this video to see if you set your constraints right: https://www.youtube.com/watch?v=4oCWxHLBQ-A – ghashi Oct 27 '16 at 03:56
  • I would move your [self populate]; method to viewWillAppear –  Oct 27 '16 at 12:26
  • @onnoweb Yes. Sorry, I didn't mention it here. However, I have included that already. I will update it. – Ron Oct 27 '16 at 13:39
  • @ghashi Thanks for the help. However, in the video the height of the ScrollView is fixed. However, for me, its dynamic. Some subviews inside the view needs to be re-calculated when you tap on it. Their height increases. How to combat this? – Ron Oct 27 '16 at 14:25
  • @JoshuaHart ... It still doesn't fix the problem. I tried your solution. – Ron Oct 27 '16 at 16:17
  • I think....perhaps your issue is with your constraints being in viewDidLayoutSubviews...check this out: http://stackoverflow.com/questions/29340904/what-will-trigger-viewdidlayoutsubviews-and-look-for-a-good-pattern-for-initing –  Oct 27 '16 at 19:15
  • Have you tried using [self.tableView reloadData]; ? –  Oct 27 '16 at 19:17

2 Answers2

0

I created this project just for your question to show how to update a scrollView just using AutoLayout (no need to override viewWillLayoutSubviews, update the scrollView's contentSize, call layoutIfNeeded or updateConstraints)

Hope it helps you =)

https://github.com/ghashi/ScrollViewQuestion/tree/master

This is the result:

shopping_list1

ghashi
  • 1,497
  • 1
  • 13
  • 17
  • @gashi...Thanks a ton to lend your helping hand. I will definitely give this a try and get back to you as soon as possible. – Ron Oct 31 '16 at 13:18
0

In our project we had encountered similar issue where we had to add a lot of views to a content view of a scrollview using constraints added programatically. Writing constraints for each view not only made the view controller bloated, it was also static. To add another view we had to write the constraints again.

We end up creating subclass of UIView that now managed this for us. We named this NNVerticalStackView.h,.m.

BangOperator
  • 4,377
  • 2
  • 24
  • 38
  • Thanks for the update. I will take a note of this and keep you posted, once I accomplish it. Thanks, once again. – Ron Oct 31 '16 at 18:34