我将图像视图传递给需要图像视图大小的init方法.然后调用convertRect:toView:
- (id) initWithImageView:(UIImageView*) imageView { self = [super init]; if (self) { CGRect imageViewFrame = [[imageView superview] convertRect: imageView.frame toView: self.view]; } }
这被称为:
MyViewController *viewController = [[MyViewController alloc] initWithImageView:imageView]; viewController.delegate = self; [self.tabBarController presentViewController:viewController animated:NO completion:nil];
然后是imageViewFrame
(origin = (x = 0,y = -120.22867049625003),size = (width = 750,height = 750))
imageView有一个框架
frame = (0 -60.1143; 375 375);
它的超级视图有一个框架
frame = (0 0; 375 254.5);
为什么它会扩大到x2?
在iPhone 6 plus(3x)模拟器上进一步测试使imageViewFrame成为可能
(origin = (x = 0,y = -199.30952358000002),size = (width = 1242,height = 1242))
第一次测试是在iPhone 6模拟器(2x)上完成的.
为什么convertRect:toView:以像素而不是点工作?
解决方法
在你的-initWithImageView:中,你的self.view尚未生成,你可以尝试临时缓存imageView,并在-viewDidLoad中执行rect转换操作:
// If you don't user Interface Builder - (void)loadView { CGRect frame = <your_view_frame>; UIView * view = [[UIView alloc] initWithFrame:frame]; self.view = view; } - (void)viewDidLoad { [super viewDidLoad]; CGRect imageViewFrame = [[imageView superview] convertRect:imageView.frame toView:self.view]; ... }
编辑
正如doc所说的-convertRect:toView:的视图参数:
The view that is the target of the conversion operation. If view is nil,this method instead converts to window base coordinates. Otherwise,both view and the receiver must belong to the same UIWindow object.
如果你坚持使用-initWithImageView方法,则声明一个自定义视图iVar,alloc&在-initWithImageView方法中初始化,然后在-loadView中,将iVar分配给self.view.当然,你在-initWithImageView中转换rect的方式应该是
CGRect frame = <your_view_frame>; _iVarView = [[UIView alloc] initWithFrame:frame]; CGRect imageViewFrame = [[imageView superview] convertRect:imageView.frame toView:_iVarView];
在-loadView中:
- (void)loadView { self.view = _iVarView; }
但是不建议以这种方式做到这一点,我建议你在正确的视图生命循环方法中初始化UI.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。