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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgeiak
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22