[英]Dynamically changing position of a UIView
这是我的设置。 我有一个正在创建并添加为子视图的视图控制器。 视图控制器提供了一些用户可以选择的选项。 响应于“长按”手势,按下了视图控制器。 在视图控制器内,我添加了一个子UIView来将其他一些控件分组在一起,以便可以将它们作为一个整体在屏幕上移动,并在它们显示时将它们居中放置在长按位置上。 这是实例化视图控制器,更改其位置并将其添加为子视图的代码:
UserOptions *opts = [[UserOptions alloc] initWithNibName:@"UserOptions" bundle:nil];
[opts recenterOptions:location];
[self.view addSubview:opts.view];
这段代码确实创建并推送了viewcontroller,但是对lasterOptions的调用没有任何作用。 这是该方法:
- (void) recenterOptions:(CGPoint)location {
CGRect oldFrame = self.optionsView.frame;
CGFloat newX = location.x; // + oldFrame.size.width / 2.0;
CGFloat newY = location.y; // + oldFrame.size.height / 2.0;
CGRect newFrame = CGRectMake(newX, newY, oldFrame.size.width, oldFrame.size.height);
self.optionsView.frame = newFrame;
}
注意self.optionsView是我添加到viewcontroller的笔尖的子UIView。
有谁知道为什么我无法更改UIView的位置?
问候,埃里克
几件事。 首先,尝试在调用-recenterOptions之前将视图添加到视图层次结构:
UserOptions *opts = [[UserOptions alloc] initWithNibName:@"UserOptions"
bundle:nil];
[self.view addSubview:opts.view];
[opts recenterOptions:location];
接下来,只需设置视图的中心,而不要尝试更改其框架:
- (void) recenterOptions:(CGPoint)location {
[[self optionsView] setCenter:location];
}
您是否在验证optionsView是否有效(非nil)?
我同意罗布,顺便说一句。 您需要更改标题以匹配您的问题。
视图被延迟加载。 在调用self.view
,不会加载视图并且optionsView为nil,因此self.optionsView.frame = newFrame
不会执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.