【发布时间】:2013-10-21 17:23:50
【问题描述】:
所以我想使用以下代码将子视图 (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 - 我要添加子视图的主视图(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}。详细说明您的视图层次结构以及您如何测试结果值。
标签: iphone ios objective-c