iOS View gets reset on addSubview

心不动则不痛 提交于 2019-12-07 14:47:55

问题


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];

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.



来源:https://stackoverflow.com/questions/22804966/ios-view-gets-reset-on-addsubview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!