ViewDidLayoutSubviews gets infinitely after switching to xcode 6

My perfectly working iOS app stopped working fine after switching to xcode 6. I found out that in one of my viewDidLayoutSubviews controllers, the method gets infinitely until the memory level reaches some point where the app crashes because of it.

Does anyone have any idea why this might be happening? I also noticed that some of my views started to look strange after switching to xcode 6, but I fixed these issues in storyboard.

Any help is greatly appreciated!

EDIT 1.

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if (self.showsPlayerControls == NO) {
        self.playerControlsView.hidden = YES;
        CGRect graphFrame = self.view.bounds;

        graphFrame.size.height = graphFrame.size.height - 20.0f;
        graphFrame.origin.y = 20.0f;

        self.graphView.frame = graphFrame;   
    }

    [self.graphView reloadData];
}

      

EDIT 2.

My fix is ​​this: basically move my code to viewWillAppear.

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.showsPlayerControls == NO) {
        self.playerControlsView.hidden = YES;
        CGRect graphFrame = self.view.bounds;

        graphFrame.size.height = graphFrame.size.height - 20.0f;
        graphFrame.origin.y = 20.0f;

        self.graphView.frame = graphFrame;
    }

    [self.graphView reloadData];
}

      

Is this a good equivalent of what I want to achieve?

+3


source to share


3 answers


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.showsPlayerControls == NO) {
        self.playerControlsView.hidden = YES;
        CGRect graphFrame = self.view.bounds;

        graphFrame.size.height = graphFrame.size.height - 20.0f;
        graphFrame.origin.y = 20.0f;

        self.graphView.frame = graphFrame;
    }

    [self.graphView reloadData];
}

      



0


source


Setting the subview frame triggers a new layout. NSLayoutConstraint

mean that there is no longer a fixed hierarchy of how layout propagation is propagated.

Quick fix:



if (!CGRectEqualToRect(self.graphView.frame, graphFrame))
    self.graphView.frame = graphFrame;   

      

If it reloadData

adds or subtracts subviews, it will have the same effect. If you are reloading data only because of a frame change, do so only when you configure the frame. If you are using viewDidLayoutSubviews

to output something other than what the subzones have been allocated, stop.

0


source


I had the same problem. When I set the UITableview's headerView as the currently updated headerView, the viewDidLayoutStubviews gets infinitely. So I forced it so that setting the headerView only happens once with the Bool variable.

var headerViewSet = false
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        commentsForRejected.numberOfLines = 0
        commentsForRejected.text = "ABCDSAFMLDSFMWL W MELWFKMLWKFMLAWEKFM AWFMALWEFKMWELKFMLAWKF ANFOAWEFNAJWEFKNAEW AFNWKEFNKAWJFNEW"
        self.headerView.frame = CGRect(x: 0,y: 0,width: headerView.frame.size.width, height: headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height)
        if headerViewSet == false {
            tableView.tableHeaderView = self.headerView
            headerViewSet = true
        }
    }

      

0


source







All Articles