UIView addSubview auto layout not working

前端 未结 3 624
Happy的楠姐
Happy的楠姐 2021-01-19 23:46

I created a UIView programatically and add NSLayoutConstraint to it and it works and then add it as a subView of view controller\'s view. Right now I need to remove this vie

3条回答
  •  盖世英雄少女心
    2021-01-20 00:15

    You need to take care of some points while adding and removing views from auto layout.

    There are two methods available in UIView.h class

    // Allows you to perform layout before the drawing cycle happens. 
    //   -layoutIfNeeded forces layout early
    - (void)setNeedsLayout;
    - (void)layoutIfNeeded;
    

    Use these methods as per your requirement. So i suggest when ever you remove subview from view, Update constraint of superior view if it is required. And then call below method to reflect layout changes.

    [self.view layoutIfNeeded];
    

    Most cases above method will solve all layout related issues. However some time if complex structure is there i mean runtime changing behaviour of views.

    Then override below method in your UIViewController, Perform all frame related changes in this method.

    - (void)viewDidLayoutSubviews
    

提交回复
热议问题