开发者

Why is this UIImageView autocentering itself?

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
        开发者_JAVA百科                       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.


ImageScrollView is the one centering your UIImageView. Set a breakpoint in ImageScrollView layoutSubviews and you'll see how your UIImageView is being centered.

You're taking ImageScrollView's internal imageView and placing it into another view. That's not going to work because ImageScrollView still retains ownership of that UIImageView instance and is still managing its layout.

You'll either need to copy the image into another UIImageView instance, or you'll need to change ImageScrollView to allow it to relinquish ownership of its imageView.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜