当新视图添加到层​​次结构时,在屏幕上调整大小和重新定位自身 [英] View on screen Resizing and Repositioning itself when new View is added into hierarchy

查看:47
本文介绍了当新视图添加到层​​次结构时,在屏幕上调整大小和重新定位自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我正在屏幕上移动用户想要移动的任何地方的A"视图(从故事板添加),我正在使用事件处理方法移动视图并且工作正常,

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,

我现在遇到的问题是,当我将新的B"视图(以编程方式)添加到 ViewControllers 视图时,我移动到其他位置的A"视图会自动移动到其默认位置.(在故事板上的位置).

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).

所以我想,我已经找到了原因,每当我们向视图层次结构中添加新视图 viewWillLayoutSubViews: get 的调用并将视图重新定位为其默认值时.所以我想知道如何防止这种情况发生?

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?

有关详细信息,请参见下图:

注意:

  1. A 和 B(橙色和红色是 UIView)

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

橙色视图是从故事板添加的,红色视图是通过编程添加的.

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

另外参见我之前关于类似问题的问题,

Also see my previous question about the similar kind problem,

任何帮助将不胜感激.

推荐答案

如果您使用 autoLayout,则必须为场景中的所有视图添加约束.
由于您没有为 view A 添加任何约束,系统将在内部添加约束以将视图放置在该特定位置.如果您随后使用框架手动移动视图,这将不会成立,因为约束将强制视图移动到其原始位置.

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. 手动向场景中的所有视图添加约束,无论是在故事板中还是以编程方式添加.每个约束都有一个 constant 属性.如果您需要调整视图的大小或位置,请更改此 constant 的值.
  2. 从场景中完全禁用自动布局.然后,您可以通过更改框架来重新定位或调整视图大小.
  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.

希望这有帮助!:)

这篇关于当新视图添加到层​​次结构时,在屏幕上调整大小和重新定位自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆