iOS - Updating auto layout constraint constant stops working after pushing another view controller then returning

醉酒当歌 提交于 2020-01-04 06:00:41

问题


I have a UIViewController designed in IB (Storyboard) that has a UIView containing a few controls. The UIView has an auto layout constraint to fix its height. In my UIViewController subclass I have an IBOutlet NSLayoutConstraint (ivar) wired to the height constraint so that I can adjust it in code. Depending on user events, I expand/shrink the height of the UIView by modifying the constraint's constant property between 80 and 44, which also slides up/down a UITableView which is constrained to the bottom of the UIView. I have used this technique before and all works well.

I am now using 4 instances of this UIViewController within a parent container UIViewController with a UITabBar. When the UITabBar tab selection is changed, I swap in the appropriate instance using insertSubView. The problem is that the UIView fails to expand/shrink in the first child view controller tab page. In the other 3 tab page instances it works fine. Also in the 3 tab pages where it works, if I push another view controller onto the stack and then return back, the resizing of the UIView no longer works.

In the case where the expand/shrink fails, I have used NSLog to monitor the value of the constraint's constant property and it is being changed in every instance, but the height of the UIView is not being affected. As if the constraint is no longer set to work on the UIView??

I'm using Xcode 5.1 and my app is targetting iOS7.


回答1:


Finally solved this problem by changing the code within the animateWithDuration block which calls layoutIfNeeded on the container control to refresh the position of the controls. Basically instead of just calling layoutIfNeeded on the parent container control, I now call layoutIfNeeded on each of the child controls whose position will change due to the constraint constant being modified. Now all controls reposition correctly on all 4 tab pages and even after returning from another view controller.



来源:https://stackoverflow.com/questions/25033653/ios-updating-auto-layout-constraint-constant-stops-working-after-pushing-anoth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!