• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS 视图在 addSubview 上重置

用户头像
it1352
帮助1

问题说明

我有一个故事板,中间有一个方形 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"?

正确答案

#1

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

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
系列文章
更多 icon
同类精品
更多 icon
继续加载