Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加UIView时CGRect位置错误_Uiview_Addsubview_Cgrect - Fatal编程技术网

添加UIView时CGRect位置错误

添加UIView时CGRect位置错误,uiview,addsubview,cgrect,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]; 无论

这是我的密码

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

无论我在CGRect中输入什么值,都会使视图仍然位于baseView的顶部。我做错了什么?

发生这种情况的原因是,您为testView提供了viewControllers视图的rect,该视图无法修改,因为它是该ViewController的主视图,即使使用initWithFrame,它也始终是相同的:

因此,您所采用的方式将无法按照您的意愿工作。您必须创建一个新的UIView,如下所示:

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

UIViewController会自动创建UIView,不能修改。

你说的“在顶部”是什么意思?你知道它的y点比你的基本视图低吗?还是在它之前?也许如果你添加一个我能理解的图像。对不起,英语不好,我的意思是我希望子视图添加一个更大的Y值,视图应该更接近“屏幕底部”。