UIView addSubview auto layout not working

前端 未结 3 622
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

    The problem is the sub view does not inherit the original view's size/frame, and therefore adds the view as if it was stuck in the Any+Any size of 600x600. My solution, is to set the frame of the original view, and then let iOS do the remainder of the work.

    [self.myOtherView setFrame:self.view.frame];   //Replace frame size with already resized view
    [self.myOtherView setNeedsLayout];  //Tell iOS to autolayout the new sub view
    [self.addSubView:self.myOtherView];  //Add it
    

提交回复
热议问题