所以我想子视图(的UIImageView)添加到我的主要观点有以下代码:设置子视图的初始位置
NSString *fileLocation = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"background_main.png"];
_aImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
_aImageView = [[UIImageView alloc] initWithImage:_aImage];
_aImageView.frame = CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height);
[self.view addSubview:_aImageView];
,你可以看到 - 我加入的UIImageView到位置(-200 ; 0);
但它显示在默认位置:(0; 0)。
我的视图层次:
- self.view -main视图,其中我添加子视图(的UIImageView);
- _aImageView - 我添加的UIImageView。
结果:
NSLog(@"image is %@",_aImageView); //result (-200 0; 420 568)
CGPoint test = [_aImageView convertPoint:_aImageView.frame.origin toView:self.view];
NSLog(@"convert %f %f",test.x,test.y); //result (-400.000000; 0.000000)
我知道那个位置仍然是(0,0),因为我可以看到它时,我测试我的应用程序。后来当我将_aImageView的位置更改为(-50; 0)时,它显示黑屏。我知道我错过了一些非常重要的东西,但我无法弄清楚为什么它出现在(0; 0)的位置。
链接类似的问题,我已经检查:
在哪个视图中的位置{0,0}。提供视图层次结构的一些细节以及如何测试结果值。 – Wain