添加UIView时CGRect错误的位置

时间:2013-09-20 12:06:44

标签: uiview addsubview cgrect

这是我的代码

CGRect testr = CGRectMake(1, 50, 320, 200);
WeatherViewController *tv = [[WeatherViewController alloc] initWithNibName:@"WeatherViewController" bundle:nil];
UIView *testview = [tv.view initWithFrame:testr];
[self.view addSubview:testview];

无论我在CGRectMake中放置什么值,View仍然最终位于我的baseView顶部。我做错了什么?

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是因为你给了testView viewControllers视图的rect,这个视图无法被修改,因为它是该ViewController的mainView,即使你使用initWithFrame也总是相同的:

所以你完成它的方式不会像你想要的那样工作。你不得不像这样创建一个新的UIView:

UIView *testView = [[UIView alloc] initWithFrame:frame];

UIViewController自动创建UIView,无法修改。