iOS 视图在 addSubview 上重置 [英] iOS View gets reset on addSubview

查看:87
本文介绍了iOS 视图在 addSubview 上重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个故事板,中间有一个方形 imageView:280,140(垂直)当应用程序启动时,我可以通过按下按钮来移动这个 imageView.我使用以下方法移动 imageView:

I have a storyboard with a square imageView in the middle at: 280,140 (vertical) When the application starts i am able to move this imageView by pressing buttons. I move the imageView using:

    _mainImage.frame = CGRectMake(_mainImage.frame.origin.x + 2, _mainImage.frame.origin.y, _mainImage.frame.size.width, _mainImage.frame.size.height);

当应用程序运行时,我不断向主视图添加新的图像视图:

while the application is running i keep adding new imageViews to the mainView:

UIImageView *imgView;
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, yCoordinate, 50, 10)];
imgView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imgView];

每次我调用此代码时,我放置在中间并四处移动的方形 imageView 都会被设置回其起始位置.每次调用 addSubview 时,mainView 似乎都会重置".有什么方法可以在不重置"mainView 的情况下添加子视图?

each time i call this code the square imageView i have placed in the middle and moved around, gets set back to its starting position. It seems like the mainView gets "reset" each time the addSubview gets called. Is there any way i can add a subview without having the mainView "reset"?

推荐答案

很可能您的视图是使用自动布局设置的(这是故事板或笔尖的默认设置),并且当您添加新的子视图时,会执行布局传递将您的视图位置重置回其原始约束定义的位置.

Most likely your view is set up using Autolayout (this is the default for storyboards or nibs) and when you add a new subview, a layout pass is performed which resets your views position back to that defined by its original constraints.

您应该通过更新约束来移动视图,或者关闭自动布局.有很多帖子解释了如何做这些事情.

You should move your view by updating its constraints, or turn off Autolayout. There are lots of posts around explaining how to do either of these things.

这篇关于iOS 视图在 addSubview 上重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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