为什么这个 UIImageView 会自动居中?
我在另一个 UIScrollView 中的 UIScrollView 中有一个 UIImageView (基于 Apple 的 PhotoScroller 示例代码)。当 UIScrollView 回调到其控制器以关闭自身时,它会调用此方法:
- (void)dismiss {
[scrollView removeFromSuperview];
ImageScrollView *isv = [self currentImageScrollView];
UIImage *image = isv.imageView;
image.frame = [self.view convertRect:image.frame fromView:isv];
[self.view insertSubview:image belowSubview:captionView];
[(NSObject *)delegate performSelector:@selector(scrollViewDidClose:)
withObject:self
afterDelay:2.0];
}
现在这里有一个奇怪的部分:在该方法执行之后,但在 scollViewDidClose
方法获取之前,图像视图立即跳转到不同的位置拜访了代表。如果图像大于其新的超级视图,则它会跳跃,使其左边缘与其超级视图的左边缘对齐。如果它比新的超级视图小,它就会跳到视图的正中心。此更改没有动画。
所以我的问题是,我如何防止它这样做?我已经调整了超级视图 (self.view
) 类和图像视图类,以查看可能调用哪些方法。调用此方法后,图像视图上既不会设置框架也不会设置中心,而在超级视图上调用 layoutSubviews
方法时,这并不是跳转的内容图像到超级视图的中心或左侧。我还尝试在超级视图中关闭 autoResizesSubviews
,并将图像视图的 autoresizingMask
设置为 UIViewAutoresizingNone
,行为没有变化。
那么这种情况发生在哪里,为什么?更重要的是,我该如何让它停止?
我已经为此绞尽脑汁好几天了。任何指示或指导将不胜感激。
I have a UIImageView in a UIScrollView in another UIScrollView (based on Apple's
PhotoScroller sample code). When the UIScrollView calls back to its controller to dismiss itself, it calls this method:
- (void)dismiss {
[scrollView removeFromSuperview];
ImageScrollView *isv = [self currentImageScrollView];
UIImage *image = isv.imageView;
image.frame = [self.view convertRect:image.frame fromView:isv];
[self.view insertSubview:image belowSubview:captionView];
[(NSObject *)delegate performSelector:@selector(scrollViewDidClose:)
withObject:self
afterDelay:2.0];
}
Now here's the weird part: the image view jumps to a different position right after this method executes, but before the scollViewDidClose
method gets called on the delegate. If the image is larger than its new super view, it jumps so that its left edge is aligned with the left edge of its super view. If it's smaller than its new super view, it jumps to the very center of the view. There is no animation to this change.
So my question is, how do I prevent it from doing that? I've tweaked both the super view (self.view
) class and the image view class to see what methods might be called. Neither the frame nor the center is set on the image view after this method is called, and while the layoutSubviews
method is called on the super view, that is not what jumps the image to the center or left side of the superview. I've also tried turning off autoResizesSubviews
in the super view, and setting the autoresizingMask
of the image view to UIViewAutoresizingNone
, with no change in behavior.
So where is this happening, and why? And more importantly, how do I make it stop?
I've been beating my head on this for days. Any pointers or guidance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ImageScrollView
是以 UIImageView 为中心的。在ImageScrollView layoutSubviews
中设置断点,您将看到 UIImageView 如何居中。您正在获取
ImageScrollView
的内部imageView
并将其放入另一个视图中。这是行不通的,因为ImageScrollView
仍然保留该UIImageView
实例的所有权,并且仍在管理其布局。您需要将图像复制到另一个
UIImageView
实例中,或者需要更改ImageScrollView
以允许其放弃其imageView
的所有权代码>.ImageScrollView
is the one centering your UIImageView. Set a breakpoint inImageScrollView layoutSubviews
and you'll see how your UIImageView is being centered.You're taking
ImageScrollView
's internalimageView
and placing it into another view. That's not going to work becauseImageScrollView
still retains ownership of thatUIImageView
instance and is still managing its layout.You'll either need to copy the image into another
UIImageView
instance, or you'll need to changeImageScrollView
to allow it to relinquish ownership of itsimageView
.当您将“图像”视图作为子视图插入时,您没有设置它的框架。如果您希望视图出现在滚动视图中的特定位置,您可能需要明确地执行此操作。
You're not setting up the frame of the 'image' view when you insert it as a subview. You probably want to do that explicitly if you want the view to appear at a particular position in the scroll view.