我将 ImageView 传递给需要该 ImageView 大小的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);
它的 super View 具有一个框架
frame = (0 0; 375 254.5);
为什么要按比例放大2倍?
在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];
...
}
编辑
正如文档所说的
-convertRect:toView:
的view param:如果您坚持使用
-initWithImageView
方法,请在-initWithImageView
方法中声明一个自定义 View iVar,alloc&init,然后在-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;
}
但是不建议这样做,我建议您以正确的 View 生命周期方法初始化UI。
关于ios - convertRect : toView: returns double size and twice the distance from the origin,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29842391/