0

I have this problem, i am moving a 'A' View on screen wherever user want's to move (Which is added from storyboard), i am moving View using event-handling methods and that is working fine,

What problem i have now is, when i add new 'B' View (Programatically) to ViewControllers view, my 'A' View which i have moved to some other position, moves automatically to its default position. (At the position where it is on storyboard).

So i think, i have figured out the reason that whenever we add new view to the view hierarchy viewWillLayoutSubViews: get's call and its reposition the views to their default. So i want to know that how can i prevent this?

See below image for more details:

enter image description here

Note:

  1. Both A and B (Orange and Red are UIView)

  2. Orange view is added from storyboard and red view is added programatically.

Also see my previous question about the similar kind problem,

Any help would be appreciated.

Community
  • 1
  • 1
Bharat Modi
  • 4,158
  • 2
  • 16
  • 27

2 Answers2

0

Yesterday I experienced same problem. If you change the position of view (A in your example) runtime created in xib or storyboard with Autolayout constraints, then add some control to same subview, view A will be positioned to original constraints.

There are 2 solutions as far as I found:

  1. viewA.translatesAutoresizingMaskIntoConstraints = YES; after initialising viewA
  2. Create your viewA programatically without constraints.
icould
  • 315
  • 3
  • 9
  • I will try and get back to you. – Bharat Modi Mar 09 '16 at 07:59
  • I'm not using Auto layouts for A view, please refer this link http://stackoverflow.com/questions/35771409/create-reusable-draggable-view for more details about the problem i'm facing. – Bharat Modi Mar 10 '16 at 07:14
  • Ok. I read your real problem in link. First of all I must say that I experienced same problem and I fixed it with above answer, however I am not still 100% satisfied. If I can see your code I may help, just screenshots are not helping Anyway I will try to create your problem in some separate app. Let you know the result. – icould Mar 10 '16 at 11:08
  • I appreciate that, thanks! Just to be clear once, 1. My AView is Added to ViewControllers view from storyboard without constraint, (other views have constraints), i have created an outlet of AView and i m moving it as you have seen in my first question link. The problem is when new view is added dynamically (which is also designed on xib and by sublcassing UIView) my AView position gets change to the default one, that is where it was added on storyboard. – Bharat Modi Mar 10 '16 at 11:29
  • I tried removing **AutoLayout** from moving view, but result was same. As the container view is using AutoLayout, I think all sub views are effected somehow. See similar question/solution in [this link](http://stackoverflow.com/questions/16129988/ios-dragged-view-is-jumping-back-to-original-position-auto-layout-combined?rq=1) – icould Mar 11 '16 at 10:19
  • Appreciate help, will try and get back to you. – Bharat Modi Mar 11 '16 at 10:30
0

If you are using autoLayout, you must add constraints for all the views in the scene.
Since you are not adding any constraints for the view A, the system will add constraints internally to place the view at that specific position. If you then move the view manually using frames, this will not hold as the constraints will force the view to move to its original position.

You can approach this problem in two ways:

  1. Manually add constraints to all views in the scene, whether it is in storyboard or added programatically. Each constraint has a constant attribute. Change the value of this constant if you need to resize or reposition the view.
  2. Disable autolayout altogether from the scene. You may then reposition or resize views by changing frame.

Hope this helps! :)

GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • I have thinked about this (Changing frames in auto layout environment is not correct way, but in that case i don't have an idea about how to change the constant of constraint which are set programatically), but then how can i change constant of specific constraint, i know how to change constant of constraint if it is set on IB (by making an outlet of the constraint.), could you please tell me how can i approach this. Thanks in advance. – Bharat Modi Apr 11 '16 at 05:08
  • @BharatModi before adding constraints programmatically, create a handle for them by adding them as properties so that you can access them later. suppose you have a property called `topConstraint`. You can change its value later like `topConstraint.constant = NEW_VALUE`. – GoGreen Apr 12 '16 at 04:20
  • It would be really good if you could provide me some code, like if i have an leadingSpaceConstraint which is set programatically that can be updated later. How to do that. – Bharat Modi Apr 12 '16 at 10:31
  • Please update the question with the code you used to add the constraints. I shall update my answer with the updated code after that. – GoGreen Apr 12 '16 at 13:54